|
1 | 1 | ---
|
2 |
| -description: "Learn more about: How to call an event handler in Visual Basic" |
3 |
| -title: How to call an event handler |
| 2 | +description: "Learn more about: How to subscribe to events and handle them in Visual Basic" |
| 3 | +title: How to subscribe to events and handle them |
4 | 4 | ms.date: 07/20/2015
|
5 | 5 | helpviewer_keywords:
|
6 | 6 | - "Visual Basic code, procedures"
|
7 |
| - - "event handlers [Visual Basic], calling" |
| 7 | + - "event handlers [Visual Basic], subscribing" |
8 | 8 | - "event handlers"
|
9 | 9 | - "procedures [Visual Basic], event handlers"
|
10 |
| - - "procedures [Visual Basic], calling" |
| 10 | + - "events [Visual Basic], subscribing" |
11 | 11 | no-loc: [WithEvents]
|
12 | 12 | ms.assetid: 72e18ef8-144e-40df-a1f4-066a57271e28
|
13 | 13 | ---
|
14 |
| -# How to call an event handler in Visual Basic |
| 14 | +# How to subscribe to events and handle them in Visual Basic |
15 | 15 |
|
16 | 16 | 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.
|
17 | 17 |
|
18 |
| -An event handler in Visual Basic 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`](../../../language-reference/statements/handles-clause.md) clause and a [`WithEvents`](../../../language-reference/modifiers/withevents.md) variable, or with an [AddHandler Statement](../../../language-reference/statements/addhandler-statement.md). Using a `Handles` clause is the default way to declare an event handler in Visual Basic. 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. |
| 18 | +In Visual Basic, there are two sides to working with events: |
| 19 | + |
| 20 | +- **Event publishing** — Classes declare events and raise them when something interesting happens using the [RaiseEvent Statement](../../../language-reference/statements/raiseevent-statement.md). This is what actually invokes (calls) the event handlers. |
| 21 | +- **Event subscription** — You subscribe to events by identifying procedures as handlers for specific events. You can do this either with a [`Handles`](../../../language-reference/statements/handles-clause.md) clause and a [`WithEvents`](../../../language-reference/modifiers/withevents.md) variable, or with an [AddHandler Statement](../../../language-reference/statements/addhandler-statement.md). |
| 22 | + |
| 23 | +An event handler in Visual Basic is a `Sub` procedure. Your code doesn't call it directly like other `Sub` procedures. Instead, event publishers invoke the procedure when the event is raised because the procedure is subscribed to the event. |
| 24 | + |
| 25 | +Using a `Handles` clause is the default way to subscribe to events in Visual Basic. This is how event handlers are written by the designers when you program in the integrated development environment (IDE). The `AddHandler` statement is suitable for subscribing to events dynamically at run time. |
19 | 26 |
|
20 | 27 | When the event occurs, Visual Basic automatically calls the event handler procedure. Any code that has access to the event can cause it to occur by executing a [RaiseEvent Statement](../../../language-reference/statements/raiseevent-statement.md).
|
21 | 28 |
|
22 | 29 | 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](../events/index.md).
|
23 | 30 |
|
24 |
| -## Call an event handler using :::no-loc text="Handles"::: and WithEvents |
| 31 | +## Subscribe to an event using :::no-loc text="Handles"::: and WithEvents |
25 | 32 |
|
26 | 33 | 1. Make sure the event is declared with an [Event Statement](../../../language-reference/statements/event-statement.md).
|
27 | 34 |
|
28 | 35 | 2. Declare an object variable at module or class level, using the [`WithEvents`](../../../language-reference/modifiers/withevents.md) keyword. The `As` clause for this variable must specify the class that raises the event.
|
29 | 36 |
|
30 | 37 | 3. In the declaration of the event-handling `Sub` procedure, add a [`Handles`](../../../language-reference/statements/handles-clause.md) clause that specifies the `WithEvents` variable and the event name.
|
31 | 38 |
|
32 |
| -4. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to make the event occur. |
| 39 | +4. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to raise the event and invoke all subscribed handlers. |
33 | 40 |
|
34 | 41 | The following example defines an event and a `WithEvents` variable that refers to the class that raises the event. The event-handling `Sub` procedure uses a `Handles` clause to specify the class and event it handles.
|
35 | 42 |
|
36 | 43 | :::code language="vb" source="snippets/how-to-call-an-event-handler/SpecialForm.vb" id="4":::
|
37 | 44 |
|
38 |
| -## Call an event handler using AddHandler |
| 45 | +## Subscribe to an event using AddHandler |
39 | 46 |
|
40 | 47 | 1. Make sure the event is declared with an `Event` statement.
|
41 | 48 |
|
42 | 49 | 2. Execute an [AddHandler statement](../../../language-reference/statements/addhandler-statement.md) to dynamically connect the event-handling `Sub` procedure with the event.
|
43 | 50 |
|
44 |
| -3. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to make the event occur. |
| 51 | +3. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to raise the event and invoke all subscribed handlers. |
45 | 52 |
|
46 | 53 | The following example uses the [AddHandler statement](../../../language-reference/statements/addhandler-statement.md) in the constructor to associate the `OnTimerElapsed` procedure as an event handler for a custom timer event.
|
47 | 54 |
|
|
0 commit comments