User control click event not working when clicking on text inside control?
Asked Answered
P

5

9

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

Pigfish answered 13/3, 2012 at 1:13 Comment(5)
UI framework (Winforms, WPF, Webforms, MVC)?Discoloration
Also please avoid including tags that have little to nothing to do with your question (e.g. visual-studio).Discoloration
By design, the Label control gets the click. You could override OnPaint() instead of using a label as one workaround.Anthropogeography
no the label doesnt respond to the event, but the background does...Pigfish
Why are you opposed to adding a single event handler with all of the references to it necessary? You could possibly even use reflection to assign the event handler.Discoloration
D
16

If I am understanding you properly, your GameButton usercontrol will fire the event when clicked on, but not when the label is clicked on -- and you want both. This is because the label (a control) is on top of the background. Therefore, you need to register your label with the click event as well. This can be done manually in the designer or programmatically for each control on the page.

If you want to do EVERY control in the UserControl, put this into the UserControl's OnLoad event and you can use the same click event for every control:

foreach (var 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 the click event handler property in the user control. This way, every time you add/remove a click event to your user control, it adds/removes it to all the controls within the user control automatically.

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 as per another post:

Hope this helps!

Diphase answered 13/3, 2012 at 1:39 Comment(3)
I wanted to be able to add click events to my custom buttons the way you do with regular buttons in the designer, but they don't affect the labelPigfish
I'm not sure I understand @EfeOzturkoglu. You want your usercontrol to be as if it were a regular button where you can click on it anywhere with one event handler?Diphase
yeah, like i dont want to have to add a handler to the control AND the label inside it each time, just add it once whenever i create a new instance of the buttonPigfish
B
2

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.

Bushy answered 3/5, 2020 at 10:5 Comment(1)
Thank you sir!! this is a clever solutionIctus
H
1

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

Houser answered 4/6, 2017 at 9:42 Comment(2)
Wrong solution as this will also gray out controls.Mathre
its works to me! i set enable false for all child controls in my user control!Bison
T
1

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

Tipcat answered 25/2, 2022 at 1:19 Comment(0)
N
0

enter image description here

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

enter image description here So wherever click inside user control the click event act as single event

Nymphet answered 11/1, 2022 at 7:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.