|
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() |
10 | 9 | End Sub
|
11 |
| - '</Snippet5> |
12 | 10 | End Class
|
13 | 11 |
|
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 |
19 | 18 | ' 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() |
20 | 24 | End Sub
|
21 | 25 | End Class
|
22 | 26 | '</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