Skip to content

Commit 7a45966

Browse files
CopilotBillWagner
andauthored
Fix event handler documentation terminology to distinguish between event publishing and subscription (#48309)
* Initial plan * Fix event handler documentation terminology and update references Co-authored-by: BillWagner <[email protected]> * Apply suggestions from code review * Apply suggestions from code review --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]> Co-authored-by: Bill Wagner <[email protected]>
1 parent 3fec60c commit 7a45966

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-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: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,54 @@
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+
- **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.
1926

2027
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).
2128

2229
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).
2330

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

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

2835
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.
2936

3037
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.
3138

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.
3340

3441
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.
3542

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

38-
## Call an event handler using AddHandler
45+
## Subscribe to an event using AddHandler
3946

4047
1. Make sure the event is declared with an `Event` statement.
4148

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

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.
4552

4653
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.
4754

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)