I have a custom usercontrol and I want to do something relatively simple.
When ever a numeric up down in that usercontrol’s value changes, have the main form update a display window.
This is not a problem if the NUD was not in a usercontrol but I can’t seem to figure out how to have the event handled by the mainform and not the usercontrol.
You need to create an event handler for the user control that is raised when an event from within the user control is fired. This will allow you to bubble the event up the chain so you can handle the event from the form.
When clicking
Button1on the UserControl, i’ll fireButton1_Clickwhich triggersUserControl_ButtonClickon the form:User control:
Form:
Notes:
Newer Visual Studio versions suggest that instead of
if (this.ButtonClick!= null) this.ButtonClick(this, e);you can useButtonClick?.Invoke(this, e);, which does essentially the same, but is shorter.The
Browsableattribute makes the event visible in Visual Studio’s designer (events view),Categoryshows it in the “Action” category, andDescriptionprovides a description for it. You can omit these attributes completely, but making it available to the designer it is much more comfortable, since VS handles it for you.