
120
|
Chapter 8: Delegates and Events
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
Custom Events
New in 2005. Sometimes there may be a software need to exhibit more control
over the lifetime of an event in your classes. The 2005 release of Visual Basic adds
features that let you monitor and take action each time a handler is added to an
event, each time a handler is removed from an event, and each time an event is
raised. This new feature is called custom events.
Using these custom events is easy. In your class definition, simply type:
accessModifier Custom Event eventName
and press the Enter key. (accessModifier is one of the standard access modifiers,
like
Public, that can be used with events.) Visual Studio provides the outline of
the custom event.
accessModifier Custom Event eventName As EventHandler
AddHandler(ByVal value As EventHandler)
' ----- Special code when adding handlers.
End AddHandler
RemoveHandler(ByVal value As EventHandler)
' ----- Special code when removing handlers.
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
' ----- Special code when raising the event.
End RaiseEvent
End Event
These event handlers fire when the specific event-related action happens with the
eventName event.