我有一个包含三个子控件的自定义控件(带有PictureBox控件和Label的面板),我想将子控件的所有事件发送到其父控件(自定义控件)。
我知道有很多关于这个问题的答案,但我无法用一个简单的解决方案来解决它。
这是我的例子
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");
}
}
答案 0 :(得分:2)
以下代码就是示例,此处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();
}
}
答案 1 :(得分:1)
我认为你可以做到这一点,但你应该知道不常见的事件无法链接到其他控件。
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)
{
}
}