Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

How to check if AddHandler has been done

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Benjamin - 22 Dec 2004 08:14 GMT
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
Herfried K. Wagner [MVP] - 22 Dec 2004 11:27 GMT
"Benjamin" <benjamin.tengelin@nospam.nospam> schrieb:
> 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
///

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/

Benjamin - 22 Dec 2004 12:30 GMT
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)
Herfried K. Wagner [MVP] - 22 Dec 2004 14:18 GMT
"Benjamin" <benjamin.tengelin@nospam.nospam> schrieb:
> Thanks for your post. I understand that my initial post was sligthly
> misleading.
[quoted text clipped - 10 lines]
>
> ("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.

Signature

M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/

"Jeffrey Tan[MSFT]" - 23 Dec 2004 01:11 GMT
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
Signature

Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.