Please read THIS post. I have the same problem as described in this post but I am trying to do in in VB.net rather than c#.
I am pretty sure to do this I have to use a custom event. (I used a code conversion site to get to learn about custom events.) So in the IDE when I type the following:
Public Custom Event AddRemoveAttendees As EventHandler
It expands to the following code snippet.
Public Custom Event AddRemoveAttendees As EventHandler
AddHandler(ByVal value As EventHandler)
End AddHandler
RemoveHandler(ByVal value As EventHandler)
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
End RaiseEvent
End Event
But I can't figure out what to do with it. Until today I had never heard of custom events.
The bottom line of what I want is to have the click event of a button bubble up to the container of the user control. I know that I could wrap my own event but I would at least like to understand custom events before I go farther down that road.
Seth
Best Answer
To use custom events for bubbling the events of another control, you can do like this:
In
AddHandler
andRemoveHandler
you simply propagate the call to attach or remove the given event handler to/from the control'sClick
event.To expand a bit on the use of custom events, here is another sample implementation of a custom event:
Now, as it looks above, it does little else than a regular event declaration:
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:If you don't need that kind of detailed control, I see no need to declare custom events.