To use custom events for bubbling the events of another control, you can do like this:
Public Custom Event AddRemoveAttendees As EventHandler
AddHandler(ByVal value As EventHandler)
AddHandler _theButton.Click, value
End AddHandler
RemoveHandler(ByVal value As EventHandler)
RemoveHandler _theButton.Click, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
' no need to do anything in here since you will actually '
' not raise this event; it only acts as a "placeholder" for the '
' buttons click event '
End RaiseEvent
End Event
In AddHandler
and RemoveHandler
you simply propagate the call to attach or remove the given event handler to/from the control's Click
event.
To expand a bit on the use of custom events, here is another sample implementation of a custom event:
Dim _handlers As New List(Of EventHandler)
Public Custom Event AddRemoveAttendees As EventHandler
AddHandler(ByVal value As EventHandler)
_handlers.Add(value)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
If _handlers.Contains(value) Then
_handlers.Remove(value)
End If
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
For Each handler As EventHandler In _handlers
Try
handler.Invoke(sender, e)
Catch ex As Exception
Debug.WriteLine("Exception while invoking event handler: " & ex.ToString())
End Try
Next
End RaiseEvent
End Event
Now, as it looks above, it does little else than a regular event declaration:
Public Event AddRemoveAttendees As EventHandler
It provides a similar mechanism allowing for event handlers to be attached and removed, and for the event to be raised. What the Custom event adds is an extra level of control; you get to write some code around the adding, removing and raising of the event, in which you can enforce rules, and tweak what will happen a little bit. For instance, you may want to limit the number of event handlers that are attached to your event. To achieve that you can alter the AddHandler
section from the sample above:
AddHandler(ByVal value As EventHandler)
If _handlers.Count < 8 Then
_handlers.Add(value)
End If
End AddHandler
If you don't need that kind of detailed control, I see no need to declare custom events.