This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!
Creating Event-Handling Methods at Run Time for Win Forms
You can bind an event to an event-handling method at run time. This allows you to connect event-handling methods based on conditions in code at run time.
To create an event-handling method at run time
- Open the form that you want to add an event-handling method to in your code editor.
- Add a method to your form with the method signature for the event that you want to handle.
For example, if you were handling the Click event of a Button control, you would create a method such as the following:
[Visual Basic]
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs
)
'Add event-handling method code here
End Sub
[C#]
private void Button1_Click(object sender, EventArgs e
) {
//Add event-handling method code here
}
- Add code to the event-handling method as appropriate to your application.
- Determine which form or control you want to create an event-handling method for.
- In a method within your form's class, add code that calls the appropriate AddOn method for the event that you want to handle. For example, if you are handling the Click event of a Button control, you would add code similar to the following:
[Visual Basic]
Button1.AddOnClick New System.EventHandler(AddressOf Me.Button1_Click)
[C#]
button1.AddOnClick(new EventHandler(Button1_Click));
The AddOnClick method establishes a click event-handling method for the button.
See Also
Event Handling | Introduction to Event-Handling Methods