Counting Connected Event Handlers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a customer control that inherits from System.Windows.Forms.Control. Its not the worlds most complicated control but I have a problem. I'm trying to get a count of the number of events connected to base.click. Is this possible

Thank

Al
 
Try using GetInvocationList().Length. I don't know if it will work in your
application or not but it works in mine.

Dale

Alec Dobbie said:
I have a customer control that inherits from System.Windows.Forms.Control.
Its not the worlds most complicated control but I have a problem. I'm
trying to get a count of the number of events connected to base.click. Is
this possible?
 
Hi Alec
No, it is not posible.
To be able to do that you have to have access to the event's backup field
which is a delegate and then you can get its invocation list.
But
1) usually those fields are private
The event doesn't give you access to the delegate. Event is simply set
of accessor methods (Add, Rmove and Invoke) So you never deal directly with
the delegate.

2) One can say "The first point is not a problem we can use reflection to
get the field". The problem is that the controls comming with the framework
don't have backup fields at all. They keep mapping tables for the events and
creates dynamically delegates only for the hooked events. What are those
tables how are they organized are Windows Forms internals.

Thus, the answer is NO.
--
B\rgds
100

Alec Dobbie said:
I have a customer control that inherits from System.Windows.Forms.Control.
Its not the worlds most complicated control but I have a problem. I'm
trying to get a count of the number of events connected to base.click. Is
this possible?
 
* "=?Utf-8?B?QWxlYyBEb2JiaWU=?= said:
I have a customer control that inherits from
System.Windows.Forms.Control. Its not the worlds most complicated
control but I have a problem. I'm trying to get a count of the number
of events connected to base.click.

\\\
Public Class Main
Public Shared 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 Shared Sub Goo()
End Sub
End Class

Public Class FooBar
Public Event Foo()

Public ReadOnly Property NumberOfFooHandlers() As Integer
Get
Return FooEvent.GetInvocationList().Length
End Get
End Property

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

Private Sub Moo()
End Sub
End Class
///
 
Hi Herfried,

I don't think Alec asks for that solution. Alec wants to count the event
handler of Control's Click event.
 
* "Stoitcho Goutsev \(100\) said:
I don't think Alec asks for that solution. Alec wants to count the event
handler of Control's Click event.

It's only a sample.

;-)
 
Back
Top