如何将子控件的所有事件发送到其父控件(自定义控件)?
问题描述:
我有一个自定义控件包含三个子控件(面板与PictureBox控件和标签),我想发送一个子控件的所有事件到其父控件(自定义控件)。如何将子控件的所有事件发送到其父控件(自定义控件)?
我知道有很多关于这个问题的答案,但是我不能用简单的解决方案来解决它。
这里是我的榜样
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");
}
}
答
下面的代码的例子,这里有UserControl1
和PictureBox
和Panel
他们的点击事件被钩到MainForm
I-E MyForm
作为命名。您可以根据您的要求修改它。
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();
}
}
编辑:
public partial class UserControl1 : UserControl
{
public PictureBox ChildPictureBox { get; set; }
public UserControl1()
{
InitializeComponent();
ChildPictureBox = pictureBox1;
}
//----
}
现在形式
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();
}
}
答
我认为你可以做到这一点,但你应该知道,不常见的事件不能与其他控件相关联。
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)
{
}
}
使用'winform'? –
这里是我写的关于如何将一个事件从子控件注册到父控件的示例。 http://stackoverflow.com/questions/31934376/main-control-to-close-child/31934727#31934727 如果你仍然不确定该怎么做,问,然后我会尝试并回答 – 2015-08-28 06:08:22