Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将单击事件从复合控件注册到父窗体_C#_Winforms_Events_Event Handling - Fatal编程技术网

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

C# 无法将单击事件从复合控件注册到父窗体,c#,winforms,events,event-handling,C#,Winforms,Events,Event Handling,我试图做的是创建一个复杂的控件,它有一个图片框、轨迹滑块和数字上下控件。在父窗体中,当用户单击图像时,将显示此复合控件,然后将背景色发送给该控件,然后使用该背景色设置控件中的图像。然后,如果用户单击复合控件上的图像,则会通知父窗体单击事件,然后从父窗体中删除该特定复合控件 复合控制代码 using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace ct

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

复合控制代码

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和Stack Overflow中所能找到的一切,但毫无乐趣。我的问题是:

  • 我说的对吗
  • 这是否需要在父表单中编码
  • 这是需要在复合控件cs文件中重新配置的内容吗
  • 这是否需要在两个文件中都配置

  • 我相信我有一个解决办法

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

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

    到目前为止,它每次都能工作。

    似乎您没有初始化
    PictureBox
    轨迹栏
    数字上下
    控件。比如:
    TrackBar TrackBar=newtrackbar()没有快乐不幸的是,仍然有相同的行为。