2015-11-02 18 views
0

我想要做的是創建一個複雜的控件,它有一個圖片框,軌道滑塊和數字向上控件。在父窗體中,當用戶單擊圖像時,將顯示此複合控件,然後將背景顏色發送給它,然後使用該背景顏色設置控件中的圖像。然後,如果用戶單擊複合控件上的圖像,則會向父窗體通知單擊事件,然後從父窗體中刪除該特定複合控件。無法註冊從複合控件到父窗體的點擊事件

複合控件代碼

using System; 
using System.ComponentModel; 
using System.Drawing; 
using System.Windows.Forms; 

namespace ctlClusterControlLib 
{ 
    public partial class UserControl1 : UserControl 
    { 
     private Color colImageBackground; 
     private int intThreadCount; 
     private PictureBox pictureBoxControl; // Compiler informs me that this is never assigned to and will always have its default value null. 
     private TrackBar trackBar;   // Compiler informs me that this is never assigned to and will always have its default value null. 
     private NumericUpDown numericUpDown; // Compiler informs me that this is never assigned to and will always have its default value null. 
     private string strImageToolTip1; 
     private string strImageToolTip2; 

     private static object EventSubmitKey = new object(); 

     public UserControl1() 
     { 
      InitializeComponent(); 
     } 

     public Color ImageBackground 
     { 
      get { return colImageBackground; } 
      set { colImageBackground = value; Invalidate(); } 
     } 

     public int ThreadCount 
     { 
      get { return intThreadCount; } 
      set { intThreadCount = value; } 
     } 

     [ 
      Category("Action"), 
      Description("Raised when the user clicks on the image.") 
     ] 
     public event EventHandler PictureClick 
     { 
      add { Events.AddHandler(EventSubmitKey, value); } 
      remove { Events.RemoveHandler(EventSubmitKey, value); } 
     } 

     public event EventHandler TrackBarScroll 
     { 
      add { trackBar.Scroll += value; } 
      remove { trackBar.Scroll -= value; } 
     } 

     public event EventHandler numericUpDownChange 
     { 
      add { numericUpDown.ValueChanged += value; } 
      remove { numericUpDown.ValueChanged -= value; } 
     } 

     public string ImageToolTip1 
     { 
      get { return strImageToolTip1; } 
      set { strImageToolTip1 = value; } 
     } 

     public string ImageToolTip2 
     { 
      get { return strImageToolTip2; } 
      set { strImageToolTip2 = value; } 
     } 

     private void trackBar1_Scroll(object sender, EventArgs e) 
     { 
      numericUpDown1.Value = trackBar1.Value; 
     } 

     private void numericUpDown1_ValueChanged(object sender, EventArgs e) 
     { 
      trackBar1.Value = Convert.ToInt32(numericUpDown1.Value); 
     } 

     protected override void OnPaint(PaintEventArgs pe) 
     { 
      base.OnPaint(pe); 
      Color c = Color.FromArgb(0xFF, colImageBackground); 
      pictureBox1.BackColor = c; 
     } 
    } 
} 

父窗體CS相關章節:

private void newPictureBox_Click(object sender, EventArgs e) 
    { 
     UserControl1 _UserControl = new UserControl1(); 
     PictureBox _PictureBox = (PictureBox)sender; 
     string _NewControlClusterName = "_New" + _PictureBox.Name; 

     _UserControl.Name = _NewControlClusterName; 
     _UserControl.ThreadCount = 16; 
     _UserControl.ImageBackground = _PictureBox.BackColor; 
     _UserControl.Dock = DockStyle.Top; 

     _UserControl.PictureClick += new EventHandler(ClusterControl_Click); 
     //_UserControl.TrackBarScroll += new EventHandler(GetTartanCode); 

     panel3.Controls.Add(_UserControl); 
     panel3.Controls.SetChildIndex(_UserControl, 0); 
    } 

,我有使用這種控制提高了點擊事件父窗體間歇性的問題。

我已經嘗試了所有我可以在Google和堆棧溢出中找到的東西,沒有任何快樂。我的問題是這樣的:

  1. 我是否在正確的球場?
  2. 這是什麼東西需要編碼在父窗體 cs文件?
  3. 這是需要重新配置複合控制 cs文件嗎?
  4. 這是需要配置的這兩個文件?
+0

看起來你似乎沒有初始化你的'PictureBox','TrackBar'或'NumericUpDown'控件。像:'TrackBar trackBar = new TrackBar();' – Huntt

+0

不幸的是,仍然有同樣的行爲。 –

回答

0

我相信我有一個解決方案。

我沒有做的是直接將請求分配給我想要註冊事件的控件。相反,我將它分配給一個新的控件,因此什麼都不會發生。

public event EventHandler PictureClick 
{ 
    add { pictureBox1.Click += value; } 
    remove { pictureBox1.Click -= value; } 
} 

到目前爲止,它每次都有效。