Search code examples
c#winformseventsuser-controlsevent-handling

Making a label inside a user control click through-able for all mouse events?


I have a custom button control. There is obviously a label inside it for the text, and when I click on the label it obviously doesn't trigger the buttons click, event, or mouseenter, or mouseleave. What I need to do is make the label act like its not even there (click-throughable) . What I used to do is:

base.Click += value;
foreach (Control i in Controls) {
    i.Click += value;
}

this used to work fine, but now I have my button's label be a different colco each time the mouse hovers over it, so i go over the button, it becomes one color (lets say red), then i go over the label and it becomes green, then back over to the label-free part, and its yellow. This looks pretty bad so I need it to ignore the label all together.


Solution

  • Don't use a label. Too much of a pain and far too expensive. Override the button's OnPaint() method to draw the text.