简体   繁体   English

如何将子控件的所有事件发送到其父控件(自定义控件)?

[英]How to send all events of a child control to its parent control (custom control)?

I have one custom control containing three child controls (Panel with PictureBox control and Label) and I want to send all events of a child controls to its parent control (custom control). 我有一个包含三个子控件(带有PictureBox控件和Label的面板)的自定义控件,并且我想将一个子控件的所有事件发送到其父控件(自定义控件)。

I know there are lots of answers regarding this problem, but I cannot figure out it with a simple solution. 我知道有关此问题的答案很多,但我无法通过简单的解决方案来解决。

Here is my example 这是我的例子

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        UserControl1 uc1 = new UserControl1();
        this.Controls.Add(uc1);
    }
}

public partial class UserControl1 : UserControl
{
    public PictureBox ChildPictureBox { get; set; }

    public UserControl1()
    {
        PictureBox pictureBox1 = new PictureBox();
        pictureBox1.Size = new Size(150, 150);
        pictureBox1.BackColor = Color.Red;
        pictureBox1.Click += PictureBox1_Click;
        this.Controls.Add(pictureBox1);
        ChildPictureBox = pictureBox1;

        this.Click += UserControl1_Click;
    }

    private void UserControl1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("User control click");
    }

    private void PictureBox1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("pic clicked");
    }      
}

The following code is the example, here UserControl1 has PictureBox and Panel and their click events are hooked into MainForm ie MyForm as named. 以下代码为示例,此处UserControl1具有PictureBoxPanel ,其单击事件已挂接到MainFormMyForm中。 You can modify it as per your requirements. 您可以根据需要对其进行修改。

UserControl1.cs UserControl1.cs

public partial class UserControl1 : UserControl
{
    public delegate void PictureBoxClickHandler(object sender, EventArgs e);
    public event PictureBoxClickHandler PictureBoxClick;

    public delegate void PanelClickHandler(object sender, EventArgs e);
    public event PanelClickHandler PanelClick;

    public delegate void PictureBoxDoubleClickHandler(object sender, EventArgs e);
    public event PictureBoxDoubleClickHandler PictureBoxDoubleClick;

    public delegate void PictureBoxMouseMoveHandler(object sender, MouseEventArgs e);
    public event PictureBoxMouseMoveHandler PictureBoxMouseMove;

    public UserControl1()
    {
        InitializeComponent();
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        if (PictureBoxClick != null)
        {
            PictureBoxClick(sender, e);
        }
    }

    private void panel1_Click(object sender, EventArgs e)
    {
        if (PanelClick != null)
        {
            PanelClick(sender, e);
        }
    }

    private void pictureBox1_DoubleClick(object sender, EventArgs e)
    {
        if (PictureBoxDoubleClick != null)
        {
            PictureBoxDoubleClick(sender, e);
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (PictureBoxMouseMove != null)
        {
            PictureBoxMouseMove(sender, e);
        }
    }
}

MyForm.cs MyForm.cs

public class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
        var userControl1 = new UserControl1();
        Controls.Add(userControl1);
        userControl1.PictureBoxClick += userControl1_PictureBoxClick;
        userControl1.PanelClick += userControl1_PanelClick;

        userControl1.PictureBoxDoubleClick+=userControl1_PictureBoxDoubleClick;
        userControl1.PictureBoxMouseMove+=userControl1_PictureBoxMouseMove;
    }

    private void userControl1_PanelClick(object sender, EventArgs e)
    {
        //Click: Panel on userControl1
    }

    private void userControl1_PictureBoxClick(object sender, EventArgs e)
    {
        //Click: PictureBox on userControl1
    }

    private void userControl1_PictureBoxMouseMove(object sender, MouseEventArgs e)
    {
        throw new NotImplementedException();
    }

    private void userControl1_PictureBoxDoubleClick(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

EDIT: 编辑:

public partial class UserControl1 : UserControl
{

    public PictureBox ChildPictureBox { get; set; }        

    public UserControl1()
    {
        InitializeComponent();
        ChildPictureBox = pictureBox1;
    }
    //----
}

Now in form 现在形式

public class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
        PictureBox pictureBox = userControl1.ChildPictureBox;
        //now work with pictureBox here
        pictureBox.Click += pictureBox_Click;
    }

    private void pictureBox_Click(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }
}

I think you can do this, but you should know that uncommon events cannot be linked to other controls. 我认为您可以执行此操作,但是您应该知道,罕见事件不能链接到其他控件。

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        foreach (Control control in this.Controls)
        {
            control.Click += new EventHandler(control_Click);
        }
    }

    private void control_Click(object sender, EventArgs e)
    {
        this.UserControl1_Click(sender, e);
    }

    private void UserControl1_Click(object sender, EventArgs e)
    {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM