'User control click event not working when clicking on text inside control?
I have a user control called GameButton that has a label inside it. When I add the user control to my form, and add a click event to it, its triggered when you click on the background of the custom button, but not the text in the label? How would I fix this without adding a bunch of click events inside the user controls code?
edit: UI framework: winforms
Solution 1:[1]
You can create a new method and assign all the controls to it
private void Control_Click(object sender, EventArgs e)
{
this.OnClick(e);
}
This will raise main control(or usercontrol) event.
Solution 2:[2]
Set the "enable" property of your labels "False, then mouse events will work in user control.
Solution 3:[3]
You can make the events in the controls of the User Control call the event of the User Control like that:
foreach (Control c in this.Controls)
{
c.Click += (sender, e) => { this.OnClick(e); };
c.MouseUp += (sender, e) => { this.OnMouseUp(e); };
c.MouseDown += (sender, e) => { this.OnMouseDown(e); };
c.MouseMove+= (sender, e) => { this.OnMouseMove(e); };
}
Just put it in the constructor. This way when an event is added to the User Control using polymorphism it will work
Solution 4:[4]
Here This control has 4 child control like 3 label and 1 picturebox.
so add this.onClick(e) in c# or Me.onClick(e) in vb.net on there on click event
like this
Private Sub rate_lab_Click(sender As Object, e As EventArgs) Handles rate_lab.Click
Me.OnClick(e)
End Sub
So wherever click inside user control the click event act as single event
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shervin Ivari |
| Solution 2 | |
| Solution 3 | Peter Csala |
| Solution 4 | lava |

