Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, fire event of container

I have a MyButton class that inherits from Button. On this class I have placed several other controls (Labels, Progessbar). The problem with this is that the controls on the Button make it impossible to fire the Button.Click or Button.MouseHover event. How can I achieve it that the controls on the Button are only displayed but are "event transparent": A click/hover on the label and progessbar is the same as if I clicked/hover directly on the Button (including sender and everything). Something like "inheriting the events from the parent".

class MyButton : Button
{
    Label foo = new Label();
    ProgressBar bar = new ProgessBar();
}
like image 934
blubberbernd Avatar asked Mar 22 '11 16:03

blubberbernd


1 Answers

You should derive from UserControl then have the button as a child control, and bubble up the button child's on click event.

This link is probably more than what you need, but it's a good starting point.

UPDATE As pointed out, you may not be using ASP.NET. So here is another post that talks about different custom user controls, specifically what you're after is a Composite Control. This is for Windows Forms.

like image 143
CodingGorilla Avatar answered Sep 25 '22 12:09

CodingGorilla