Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make an Event in the Usercontrol and have it handled in the Main Form?

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.

like image 280
Crash893 Avatar asked Oct 24 '11 19:10

Crash893


People also ask

How do you make the event raised by child controls in a user control available to the host page?

By default, events raised by child controls in a user control are not available to the host page. However, you can define events for your user control and raise them so that the host page is notified of the event. You do this in the same way that you define events for any class.

How do you create event handlers with visual studio net?

From the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event. Visual Studio creates the event handler and moves the insertion point to the newly created event handler.

What is an event handler example?

In general, an event handler has the name of the event, preceded by "on." For example, the event handler for the Focus event is onFocus. Many objects also have methods that emulate events. For example, button has a click method that emulates the button being clicked.


2 Answers

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 Button1 on the UserControl, i'll fire Button1_Click which triggers UserControl_ButtonClick on the form:

User control:

[Browsable(true)] [Category("Action")]  [Description("Invoked when user clicks button")] public event EventHandler ButtonClick;  protected void Button1_Click(object sender, EventArgs e) {     //bubble the event up to the parent     if (this.ButtonClick!= null)         this.ButtonClick(this, e);                } 

Form:

UserControl1.ButtonClick += new EventHandler(UserControl_ButtonClick);  protected void UserControl_ButtonClick(object sender, EventArgs e) {     //handle the event  } 

Notes:

  • Newer Visual Studio versions suggest that instead of if (this.ButtonClick!= null) this.ButtonClick(this, e); you can use ButtonClick?.Invoke(this, e);, which does essentially the same, but is shorter.

  • The Browsable attribute makes the event visible in Visual Studio's designer (events view), Category shows it in the "Action" category, and Description provides 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.

like image 184
James Johnson Avatar answered Oct 08 '22 21:10

James Johnson


Try mapping it. Try placing this code in your UserControl:

public event EventHandler ValueChanged {   add { numericUpDown1.ValueChanged += value; }   remove { numericUpDown1.ValueChanged -= value; } } 

then your UserControl will have the ValueChanged event you normally see with the NumericUpDown control.

like image 22
LarsTech Avatar answered Oct 08 '22 23:10

LarsTech