Custom click not working when clicking on text inside the control?

I have a user control called GameButton that has a label in it. When I add a user control to my form and add a click event to it, it fires when you click on the background of the user button, but not on the text in the shortcut? How can I fix this without adding a bunch of click events inside the code of user controls?

edit: UI: winforms

+6
source share
2 answers

If I understand you correctly, your GameButton user control fires an event when you click, but not when you click on a label - and you want both. This is due to the fact that the label (control) is on top of the background. Therefore, you need to register your shortcut with the click event. This can be done manually in the designer or programmatically for each control on the page.

If you want to do EVERY control in a UserControl, put it in the UserControl OnLoad event, and you can use the same click event for each control:

foreach(Control c in this.Controls){ c.Click += new EventHandler(yourEvent_handler_click); } public void yourEvent_handler_click (object sender, EventArgs e){ //whatever you want your event handler to do } 

EDIT: The best way is to create a click event handler property in a user control. That way, every time you add / remove the click event for your user control, it automatically adds / removes it to all the controls in the user control.

 public new event EventHandler Click { add { base.Click += value; foreach (Control control in Controls) { control.Click += value; } } remove { base.Click -= value; foreach (Control control in Controls) { control.Click -= value; } } } 

This is from another post:

Hope this helps!

+12
source

Set the "enable" property of your shortcuts to "False", then mouse events will work in user control.

+1
source

All Articles