views:

31

answers:

2

Is there a way to find all event handlers for a Winforms control? Specifically statically defined event handlers?

+1  A: 

Windows Forms controls use an EventHandlerList property called Events to hold event handlers so you could iterate that collection. To determine which subscribed handlers are static, you will need to use reflection.

Jeff Yates
+1  A: 

Windows Forms has strong counter-measures against doing this. Most controls store the event handler reference in a list that requires a secret 'cookie'. The cookie value is dynamically created, you cannot guess it up front. Reflection is a backdoor, you have to know the cookie variable name. The one for the Control.Click event is named "EventClick" for example, you can see this in the Reference Source or with Reflector.

This is all incredibly unpractical, if you're getting the feeling you are doing something unwise then you're on the right track. You can find sample code that does this in my answer in this thread.

Hans Passant
"if you're getting the feeling you are doing something unwise then you're on the right track". I was just starting to get that feeling when I got your answer here. Thanks. Definitely need to rethink what I'm doing and why. Thanks for the help.
Jeff