Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire parent click event when child is clicked in windows form user control

I have a user control

    public partial class ButtonControl : UserControl

which has two controls of label and picturebox

        this.pictureBox1 = new System.Windows.Forms.PictureBox();
        this.text = new System.Windows.Forms.Label();

I have used this control in a windows form

this.appointmentButton = new DentalSoft.UI.Controls.ButtonControl();

created an event

            this.appointmentButton.Click += new System.EventHandler(this.appointmentButton_Click);

enter image description here

but the problem is, if i click on the image or the label inside of the control, the click event doesn't fire. I want to fire this click event no matter where the user clicks inside of the control. Is it possible?

like image 265
Foyzul Karim Avatar asked Apr 29 '11 16:04

Foyzul Karim


1 Answers

Yes it is a simple matter. When you click on the child controls they receive the click event and the user control does not. So you can subscribe to the child control click events and when they occur simply raise the usercontrol click event and it will appear to click no matter where the mouse is positioned.

Just double click on the picturebox and the label to create the click event handlers then add a line of code to call the parent usercontrol OnClick method.

    private void text_Click(object sender, EventArgs e)
    {
        this.OnClick(new EventArgs());
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        this.OnClick(new EventArgs());
    }
like image 152
Sisyphus Avatar answered Dec 15 '22 05:12

Sisyphus