简体   繁体   中英

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).

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. You can modify it as per your requirements.

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

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)
    {

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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