How to check if AddHandler has been done

G

Guest

How can I determine if a AddHandler already has been specified for a specific
event.

Sub TestEvents()
Dim Obj As New Class1()
...
AddHandler Obj.Ev_Event, AddressOf EventHandler
...
'lets say that i HERE want to check if a AddHandler has been registered on
"Obj.Ev_Event"
Obj.CauseSomeEvent() ' Ask the object to raise an event.
End Sub

I have not understood where actually all event handler connections are
stored. In a global variabel, or on each object ?
Can I use System.ComponentModel.EventHandlerList ? How then do I access it?

Best regards,
Benjamin, Sweden
 
H

Herfried K. Wagner [MVP]

Benjamin said:
How can I determine if a AddHandler already has been specified for a
specific
event.

For your own events:

\\\
Public Module Program
Public Sub Main()
Dim c As New FooBar()
AddHandler c.Foo, AddressOf Goo
c.AddSampleHandler()
c.AddSampleHandler()
Console.WriteLine( _
"Anzahl der Handler für Foo: {0}", _
c.NumberOfFooHandlers _
)
RemoveHandler c.Foo, AddressOf Goo
Console.Read()
End Sub

Private Sub Goo()
End Sub
End Module

Public Class FooBar
Public Event Foo()

Public ReadOnly Property NumberOfFooHandlers() As Integer
Get
If FooEvent Is Nothing Then
Return 0
Else
Return FooEvent.GetInvocationList().Length
End If
End Get
End Property

Public Sub AddSampleHandler()
AddHandler Foo, AddressOf Moo
End Sub

Private Sub Moo()
End Sub
End Class
///
 
G

Guest

Thanks for your post. I understand that my initial post was sligthly
misleading.
Anyway now I know how to access my "own events". But, what I really want to
access is the list of AddHandler-connections on standard (!) events.

Lets illustrate with an example. I would like to programmatically check if
following AddHandler has been done earlier (if so I do not want do duplicate
the AddHandler):

AddHandler button.Click, AddressOf button_Click

("button" is a standard System.Windows.Forms.Button)
 
H

Herfried K. Wagner [MVP]

Benjamin said:
Thanks for your post. I understand that my initial post was sligthly
misleading.
Anyway now I know how to access my "own events". But, what I really want
to
access is the list of AddHandler-connections on standard (!) events.

Lets illustrate with an example. I would like to programmatically check if
following AddHandler has been done earlier (if so I do not want do
duplicate
the AddHandler):

AddHandler button.Click, AddressOf button_Click

("button" is a standard System.Windows.Forms.Button)

IIRC that's not possible. You'll have to keep track of what handlers you
added yourself.
 
J

Jeffrey Tan[MSFT]

Hi Benjamin,

Yes, I agree with Herfried that outside of the event definition class, we
can not use a method or property to get the hooked event chain.

Actually, because we use this class, we add the event handler to the
class's certain event, we should "know" of if the event handler is added.
Also, if we want to get the hooked event handler list, we can use an inner
list to store the hooked event handler list.

Hope this makes sense to you.

At last, I think this is more a language related issue than Winform issue,
I suggest you post this issue in "dotnet.languages.vb", then you will get
more useful feedback and help. Thanks
==========================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top