我有一个用户控件
public partial class ButtonControl : UserControl
它有两个标签和图片框控件
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.text = new System.Windows.Forms.Label();
我已经在Windows窗体中使用了此控件
this.appointmentButton = new DentalSoft.UI.Controls.ButtonControl();
创建了一个事件
this.appointmentButton.Click += new System.EventHandler(this.appointmentButton_Click);
但是问题是,如果我单击控件内的图像或标签,则不会触发click事件。
无论用户在控件内部单击什么位置,我都希望触发此click事件。是否可以?
最佳答案
是的,这很简单。当您单击子控件时,它们会收到click事件,而用户控件则不会。因此,您可以订阅子控件单击事件,当它们发生时,只需引发usercontrol单击事件,无论鼠标位于何处,它都将显示为单击。
只需双击图片框和标签以创建click事件处理程序,然后添加一行代码以调用父usercontrol OnClick方法。
private void text_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
关于user-controls - 在Windows窗体用户控件中单击子项时触发父项单击事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5834588/