I am trying to test out very simple event handling in VB.NET.
So far I have:
Public Delegate Sub TestEventDelegate()
Public Event TestEvent As TestEventDelegate
Sub MySub
Raise TestEvent
End Sub
How would you write an event handler for the above event that just displayed a simple MessageBox?
Writing the handler method is simple – just write a
Subwhich takes no parameters and displays a message box.You then need to subscribe the handler to the event, which you can either do adding a
Handlesclause to the method:Or by using an
AddHandlerstatement:Note that to follow .NET conventions, your delegate should have two parameters – one of type
Objectto specify which object raised the event, and one of typeEventArgsor a subclass, to provide any extra information. This isn’t required by the language, but it’s a broadly-followed convention.