C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
AddHandler: In Main we use the AddHandler keyword to attach EventHandlers to our Event. We create Delegate instances.
AddressOf: We use AddressOf to reference the Important1 and Important2 Subs as targets for the EventHandler delegate instances.
RaiseEvent: We use this keyword to cause an Event to be triggered (raised). The Important1 and Important2 Subs are executed.
SubVB.NET program that uses Event, AddHandler, RaiseEvent
Module Module1
Delegate Sub EventHandler()
Event _show As EventHandler
Sub Main()
' Use AddHandler to attach two EventHandlers to Event.
AddHandler _show, New EventHandler(AddressOf Important1)
AddHandler _show, New EventHandler(AddressOf Important2)
' Use RaiseEvent to run all handlers for this event.
RaiseEvent _show()
End Sub
Sub Important1()
' Do something important.
Console.WriteLine("Important1")
End Sub
Sub Important2()
' Do something else that is also important.
Console.WriteLine("Important2")
End Sub
End Module
Output
Important1
Important2
Note: In a GUI a button click will cause an event to be raised. We can run any number of methods when a click occurs.
Button