| title | How to: Call an Event Handler in Visual Basic | |||||
|---|---|---|---|---|---|---|
| ms.custom | ||||||
| ms.date | 07/20/2015 | |||||
| ms.prod | .net | |||||
| ms.reviewer | ||||||
| ms.suite | ||||||
| ms.technology |
|
|||||
| ms.topic | article | |||||
| helpviewer_keywords |
|
|||||
| ms.assetid | 72e18ef8-144e-40df-a1f4-066a57271e28 | |||||
| caps.latest.revision | 19 | |||||
| author | dotnet-bot | |||||
| ms.author | dotnetcontent |
An event is an action or occurrence — such as a mouse click or a credit limit exceeded — that is recognized by some program component, and for which you can write code to respond. An event handler is the code you write to respond to an event.
An event handler in [!INCLUDEvbprvb] is a Sub procedure. However, you do not normally call it the same way as other Sub procedures. Instead, you identify the procedure as a handler for the event. You can do this either with a Handles clause and a WithEvents variable, or with an AddHandler Statement. Using a Handles clause is the default way to declare an event handler in [!INCLUDEvbprvb]. This is the way the event handlers are written by the designers when you program in the integrated development environment (IDE). The AddHandler statement is suitable for raising events dynamically at run time.
When the event occurs, [!INCLUDEvbprvb] automatically calls the event handler procedure. Any code that has access to the event can cause it to occur by executing a RaiseEvent Statement.
You can associate more than one event handler with the same event. In some cases you can dissociate a handler from an event. For more information, see Events.
-
Make sure the event is declared with an Event Statement.
-
Declare an object variable at module or class level, using the WithEvents keyword. The
Asclause for this variable must specify the class that raises the event. -
In the declaration of the event-handling
Subprocedure, add a Handles clause that specifies theWithEventsvariable and the event name. -
When the event occurs, [!INCLUDEvbprvb] automatically calls the
Subprocedure. Your code can use aRaiseEventstatement to make the event occur.The following example defines an event and a
WithEventsvariable that refers to the class that raises the event. The event-handlingSubprocedure uses aHandlesclause to specify the class and event it handles.[!code-vbVbVbcnProcedures#4]
-
Make sure the event is declared with an
Eventstatement. -
Execute an AddHandler Statement to dynamically connect the event-handling
Subprocedure with the event. -
When the event occurs, [!INCLUDEvbprvb] automatically calls the
Subprocedure. Your code can use aRaiseEventstatement to make the event occur.The following example defines a
Subprocedure to handle the xref:System.Windows.Forms.Form.Closing event of a form. It then uses the AddHandler Statement to associate thecatchCloseprocedure as an event handler for xref:System.Windows.Forms.Form.Closing.[!code-vbVbVbcnProcedures#5]
You can dissociate an event handler from an event by executing the RemoveHandler Statement.
Procedures
Sub Procedures
Sub Statement
AddressOf Operator
How to: Create a Procedure
How to: Call a Procedure that Does Not Return a Value