Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User Control Click - Windows Forms

I have a custom user control on my windows forms. This control has a few labels on it.

I will be dynamically displaying an array of these controls on my form which will contain different bits of data.

What I am trying to do is know which user control was selected when I click on it.

This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.

Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?

If this question is not clear, or you need more info, please leave a comment.

I am doing this in c#.

Thanks!

like image 971
CodeLikeBeaker Avatar asked Feb 03 '23 10:02

CodeLikeBeaker


1 Answers

User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:

foreach (Control control in Controls)
{
    // I am assuming MyUserControl_Click handles the click event of the user control.
    control.Click += MyUserControl_Click;
}

After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.

like image 104
Serhat Ozgel Avatar answered Feb 06 '23 12:02

Serhat Ozgel