How to: Create Event Handlers at Run Time for Windows Forms
In addition to creating events using the Windows Forms Designer, you can also create an event handler at run time. This action allows you to connect event handlers based on conditions in code at run time as opposed to having them connected when the program initially starts.
Open the form in the Code Editor that you want to add an event handler to.
Add a method to your form with the method signature for the event that you want to handle.
Add code to the event handler as appropriate to your application.
Determine which form or control you want to create an event handler for.
In a method within your form's class, add code that specifies the event handler to handle the event. For example, the following code specifies the event handler
button1_Clickhandles the Click event of a Button control:
The AddHandler method demonstrated in the Visual Basic code above establishes a click event handler for the button.