无法将复合控件中的点击事件注册到父表单

时间:2015-11-02 06:18:52

标签: c# winforms events event-handling

我要做的是创建一个复杂的控件,它有一个图片框,轨道滑块和数字向下控件。在父窗体中,当用户单击图像时,将显示此复合控件,然后将背景颜色发送给它,然后使用该背景颜色设置控件中的图像。然后,如果用户单击复合控件上的图像,则会向父窗体通知click事件,然后从父窗体中删除该特定复合控件。

复合控制码

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);
    }

我使用此控件将click事件提升到父窗体时出现间歇性问题。

我已经尝试过在Google和Stack Overflow中找到的所有内容,但没有任何乐趣。我的问题是:

  1. 我是否在正确的球场?
  2. 这是否需要在父表单 cs文件中编码?
  3. 这是否需要在复合控件 cs文件中重新配置?
  4. 这是否需要在两个文件中配置?

1 个答案:

答案 0 :(得分:0)

我相信我有一个解决方案。

我没做的是直接将请求分配给我想要注册事件的控件。相反,我将它分配给一个新的控件,因此什么都不会发生。

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

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