Skip to content

Commit c5a40f8

Browse files
CopilotBillWagner
andauthored
Update Visual Basic event handler article to use generic examples instead of WinForms (#48221)
* Initial plan * Replace WinForms-specific examples with generic event handling examples Co-authored-by: BillWagner <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: BillWagner <[email protected]>
1 parent 10c6ddd commit c5a40f8

File tree

4 files changed

+71
-58
lines changed

4 files changed

+71
-58
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ You can associate more than one event handler with the same event. In some cases
4343

4444
3. When the event occurs, Visual Basic automatically calls the `Sub` procedure. Your code can use a `RaiseEvent` statement to make the event occur.
4545

46-
The following example uses the [AddHandler statement](../../../language-reference/statements/addhandler-statement.md) in the constructor to associate the `OnFormClosing` procedure as an event handler for <xref:System.Windows.Forms.Form.FormClosing>.
46+
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.
4747

4848
:::code language="vb" source="snippets/how-to-call-an-event-handler/SpecialForm.vb" id="5":::
4949

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net8.0-windows</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
66
<RootNamespace>CallEventHandler</RootNamespace>
7-
<StartupObject>CallEventHandler.SpecialForm</StartupObject>
8-
<UseWindowsForms>true</UseWindowsForms>
97
</PropertyGroup>
108

11-
<ItemGroup>
12-
<Import Include="System.Data" />
13-
<Import Include="System.Drawing" />
14-
<Import Include="System.Windows.Forms" />
15-
</ItemGroup>
16-
179
</Project>

docs/visual-basic/programming-guide/language-features/procedures/snippets/how-to-call-an-event-handler/SpecialForm.Designer.vb

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 67 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,74 @@
1-
Public Class SpecialForm
2-
'<Snippet5>
3-
Sub New()
4-
InitializeComponent()
5-
AddHandler Me.FormClosing, AddressOf OnFormClosing
6-
End Sub
7-
8-
Private Sub OnFormClosing(sender As Object, e As FormClosingEventArgs)
9-
' Insert code to deal with impending closure of this form.
1+
'<Snippet4>
2+
' Example showing event handling with Handles and WithEvents
3+
Public Class EventPublisher
4+
Public Event SomethingHappened()
5+
6+
Public Sub CauseEvent()
7+
' Raise the event when something interesting happens
8+
RaiseEvent SomethingHappened()
109
End Sub
11-
'</Snippet5>
1210
End Class
1311

14-
'<Snippet4>
15-
Public Class RaisesEvent
16-
Public Event SomethingHappened()
17-
Dim WithEvents happenObj As New RaisesEvent
18-
Public Sub ProcessHappen() Handles happenObj.SomethingHappened
12+
Public Class EventSubscriber
13+
' Declare a WithEvents variable
14+
Dim WithEvents eventObj As New EventPublisher
15+
16+
' Handle the event using Handles clause
17+
Public Sub ProcessHappen() Handles eventObj.SomethingHappened
1918
' Insert code to handle somethingHappened event.
19+
Console.WriteLine("Event handled using Handles clause!")
20+
End Sub
21+
22+
Public Sub TriggerEvent()
23+
eventObj.CauseEvent()
2024
End Sub
2125
End Class
2226
'</Snippet4>
27+
28+
'<Snippet5>
29+
' Example showing event handling with AddHandler
30+
Public Class Timer
31+
Public Event TimerElapsed(ByVal message As String)
32+
33+
Public Sub Start()
34+
' Simulate timer elapsed
35+
RaiseEvent TimerElapsed("Timer has elapsed!")
36+
End Sub
37+
End Class
38+
39+
Public Class Application
40+
Private appTimer As New Timer()
41+
42+
Sub New()
43+
' Use AddHandler to dynamically associate event handler
44+
AddHandler appTimer.TimerElapsed, AddressOf OnTimerElapsed
45+
End Sub
46+
47+
Private Sub OnTimerElapsed(ByVal message As String)
48+
' Insert code to handle timer elapsed event
49+
Console.WriteLine($"Handling timer event: {message}")
50+
End Sub
51+
52+
Public Sub StartTimer()
53+
appTimer.Start()
54+
End Sub
55+
End Class
56+
'</Snippet5>
57+
58+
' Demo program to show both event handling approaches
59+
Module Program
60+
Sub Main()
61+
' Demonstrate Handles approach
62+
Console.WriteLine("=== Demonstrating Handles approach ===")
63+
Dim subscriber As New EventSubscriber()
64+
subscriber.TriggerEvent()
65+
66+
' Demonstrate AddHandler approach
67+
Console.WriteLine("=== Demonstrating AddHandler approach ===")
68+
Dim app As New Application()
69+
app.StartTimer()
70+
71+
Console.WriteLine("Press any key to exit...")
72+
Console.ReadKey()
73+
End Sub
74+
End Module

0 commit comments

Comments
 (0)