Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom button: Click through label

I need to create custom Button - a Panel with 2 Labels inside for nice visual formatting.

But I need to click on panel through Labels to trigger an event, for example, MouseDown, or I need to assign that event to both labels.

I tried to create UserControl but problem is the same - if I set MouseDown event to UserControl, labels in it prevent to click control itself.

If I set to Label.Enable = false it resolves that problem but generate another - text on Label become gray and that property I cannot overwrite (I need black text, and red when some events occur).

like image 538
Kodek Avatar asked Dec 06 '25 17:12

Kodek


1 Answers

What you can do is to propagate your MouseDown event in your Label to call MouseDown event for your Panel

private void panel1_MouseDown(object sender, MouseEventArgs e) {
    //Do something here
}

private void label2_MouseDown(object sender, MouseEventArgs e) {
    panel1_MouseDown(sender, e);
}

To distinguish the sender you could leave the sender for your label event the same (Label) - not changing it to Panel. Just in case you would need some distinction in the panel_MouseDown.

like image 136
Ian Avatar answered Dec 08 '25 07:12

Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!