Skip to content

Commit 6321efe

Browse files
CopilotBillWagner
andcommitted
Fix event handler documentation terminology and update references
Co-authored-by: BillWagner <[email protected]>
1 parent 5b739c1 commit 6321efe

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

docs/visual-basic/misc/bc33003.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ms.assetid: 8336402c-9393-4e8e-834d-55c2268f24f6
1313

1414
An [Operator Statement](../language-reference/statements/operator-statement.md) specifies the [Handles](../language-reference/statements/handles-clause.md) keyword.
1515

16-
Only a `Sub` procedure can handle events. An `Operator` procedure cannot. For more information on event handlers, see [How to: Call an Event Handler in Visual Basic](../programming-guide/language-features/procedures/how-to-call-an-event-handler.md).
16+
Only a `Sub` procedure can handle events. An `Operator` procedure cannot. For more information on event handlers, see [How to: Subscribe to Events and Handle Them in Visual Basic](../programming-guide/language-features/procedures/how-to-call-an-event-handler.md).
1717

1818
An `Operator` procedure requires both the `Public` and `Shared` keywords, and a conversion operator requires either the `Widening` or the `Narrowing` keyword. For more information, see [Operator Procedures](../programming-guide/language-features/procedures/operator-procedures.md).
1919

docs/visual-basic/programming-guide/language-features/procedures/how-to-call-a-procedure-that-does-not-return-a-value.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ A `Sub` procedure does not return a value to the calling code. You call it expli
3434
- [Sub Statement](../../../language-reference/statements/sub-statement.md)
3535
- [How to: Create a Procedure](./how-to-create-a-procedure.md)
3636
- [How to: Call a Procedure That Returns a Value](./how-to-call-a-procedure-that-returns-a-value.md)
37-
- [How to: Call an Event Handler in Visual Basic](./how-to-call-an-event-handler.md)
37+
- [How to: Subscribe to Events and Handle Them in Visual Basic](./how-to-call-an-event-handler.md)

docs/visual-basic/programming-guide/language-features/procedures/how-to-call-an-event-handler.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
11
---
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
44
ms.date: 07/20/2015
55
helpviewer_keywords:
66
- "Visual Basic code, procedures"
7-
- "event handlers [Visual Basic], calling"
7+
- "event handlers [Visual Basic], subscribing"
88
- "event handlers"
99
- "procedures [Visual Basic], event handlers"
10-
- "procedures [Visual Basic], calling"
10+
- "events [Visual Basic], subscribing"
1111
no-loc: [WithEvents]
1212
ms.assetid: 72e18ef8-144e-40df-a1f4-066a57271e28
1313
---
14-
# How to call an event handler in Visual Basic
14+
# How to subscribe to events and handle them in Visual Basic
1515

1616
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.
1717

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+
1. **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+
22+
2. **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).
23+
24+
An event handler in Visual Basic is a `Sub` procedure. However, you do not normally call it directly like other `Sub` procedures. Instead, you subscribe the procedure to an event, and Visual Basic automatically calls the event handler when the event is raised.
25+
26+
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.
1927

2028
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).
2129

2230
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).
2331

24-
## Call an event handler using :::no-loc text="Handles"::: and WithEvents
32+
## Subscribe to an event using :::no-loc text="Handles"::: and WithEvents
2533

2634
1. Make sure the event is declared with an [Event Statement](../../../language-reference/statements/event-statement.md).
2735

2836
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.
2937

3038
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.
3139

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.
40+
4. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to raise the event and trigger all subscribed handlers.
3341

3442
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.
3543

3644
:::code language="vb" source="snippets/how-to-call-an-event-handler/SpecialForm.vb" id="4":::
3745

38-
## Call an event handler using AddHandler
46+
## Subscribe to an event using AddHandler
3947

4048
1. Make sure the event is declared with an `Event` statement.
4149

4250
2. Execute an [AddHandler statement](../../../language-reference/statements/addhandler-statement.md) to dynamically connect the event-handling `Sub` procedure with the event.
4351

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.
52+
3. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to raise the event and trigger all subscribed handlers.
4553

4654
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.
4755

docs/visual-basic/programming-guide/language-features/procedures/sub-procedures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ The following example shows a typical call to `tellOperator`.
9090
- [Procedure Parameters and Arguments](./procedure-parameters-and-arguments.md)
9191
- [Sub Statement](../../../language-reference/statements/sub-statement.md)
9292
- [How to: Call a Procedure that Does Not Return a Value](./how-to-call-a-procedure-that-does-not-return-a-value.md)
93-
- [How to: Call an Event Handler in Visual Basic](./how-to-call-an-event-handler.md)
93+
- [How to: Subscribe to Events and Handle Them in Visual Basic](./how-to-call-an-event-handler.md)

docs/visual-basic/programming-guide/language-features/procedures/toc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
items:
99
- name: "How to: Call a Procedure that Does Not Return a Value"
1010
href: how-to-call-a-procedure-that-does-not-return-a-value.md
11-
- name: "How to: Call an Event Handler"
11+
- name: "How to: Subscribe to Events and Handle Them"
1212
href: how-to-call-an-event-handler.md
1313
- name: Function Procedures
1414
href: function-procedures.md

0 commit comments

Comments
 (0)