enumerating eventhandlers

  • Thread starter Thread starter Phil
  • Start date Start date
P

Phil

Does any body know how to get a list of event handlers
assigned to a paticular event?

I'd like to be able to determine if a sub has already been
added as an event handler for a form.paint event.

while eventinfo has methods for addhandler and remove
handler I don't se a way to do a 'gethandler'
or 'enumeratehandlers' etc.

I'm trying to aviod adding a handler more than once.

Thanks
 
So, are you suggesting keeping my own list of what
handlers I assign?

if so, riddle me this batman...
do you know a way to check if and event has any handler
assigned at all, even if it was done outside the scope of
whats doing the checking?
i.e. if form.paint is nothing then .....etc
 
Phil,
Remember that events are defined in terms of Delegates. Delegates have a
GetInvocationList method to get the list of handlers attached to that
delegate, which will be the list of handlers attached to the event.

When you define an event in your class VB.NET will define a delegate field
for that event. You can use this field to get the invocation list.

Public Class Phil

' this creates a NotifyEvent field in the class.
Public Event Notify As EventHandler

Public Sub CheckNotify()
Dim list() As [Delegate] = NotifyEvent.GetInvocationList()
For Each handler As [Delegate] In list
' do something interesting here.
Next
End Sub

End Class

Hope this helps
Jay
 
Back
Top