无法将单击事件从复合控件注册到父窗体

本文关键字:注册 窗体 复合控件 单击 事件 | 更新日期: 2023-09-27 18:20:05

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

复合控制代码

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

我在使用此控件将单击事件提升到父窗体时遇到了间歇性问题。

我已经尝试了在谷歌和Stack Overflow中能找到的一切,但没有任何乐趣。我的问题是:

  1. 我在正确的球场上吗
  2. 这是否需要在父窗体cs文件中进行编码
  3. 这是否需要在复合控件cs文件中重新配置
  4. 这是否需要在两个文件中进行配置

无法将单击事件从复合控件注册到父窗体

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

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

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

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