在Windows窗体用户控件中单击子项时,触发父父点击事件

Foy*_*rim 4 user-controls delegates click event-handling windows-forms-designer

我有一个用户控件

    public partial class ButtonControl : UserControl
Run Code Online (Sandbox Code Playgroud)

它有两个标签和图片框控件

        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.text = new System.Windows.Forms.Label();
Run Code Online (Sandbox Code Playgroud)

我在Windows窗体中使用了此控件

this.appointmentButton = new DentalSoft.UI.Controls.ButtonControl();
Run Code Online (Sandbox Code Playgroud)

创造了一个活动

            this.appointmentButton.Click += new System.EventHandler(this.appointmentButton_Click);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但问题是,如果我点击图像或控件内的标签,点击事件不会触发.无论用户在控件内部单击何处,我都要触发此单击事件.可能吗?

Sis*_*hus 8

是的,这是一件简单的事情.当您单击子控件时,他们会收到click事件,而用户控件则不会.因此,您可以订阅子控件单击事件,当它们发生时,只需引发usercontrol单击事件,无论鼠标位于何处,它都会显示为单击.

只需双击图片框和标签即可创建单击事件处理程序,然后添加一行代码以调用父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());
    }
Run Code Online (Sandbox Code Playgroud)