control's event handler

Hi all,

I have a variable of type Button that I get from a call back on a form.
Just having this variable, is there a way to find the button's Click event
handler?
Just to be clear - when I assign it myself, like so:

Button b = new Button();
b.Click += new EventHandler(this.button_Click);

I obviously know that button_Click function gets called -
that's what I need to find out for the variable.

Thank you,

Gene
Mi-JackAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
That is possible with reflection.  The steps are shown in the code from this CodeProject article:

Removing Event Handlers using Reflection
http://www.codeproject.com/Articles/103542/Removing-Event-Handlers-using-Reflection
0
 
Bob LearnedCommented:
I don't understand your question, and what you mean by "that's what I need to find out for the variable"
0
 
Mi-JackAuthor Commented:
I have a variable of type Button, and I need to find out its OnClick EventHandler(s)
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Bob LearnedCommented:
Do you mean typeof(Button).GetType().GetEvents()?
0
 
Mi-JackAuthor Commented:
I mean that particular Button reference to which I have, not Button as a class
0
 
Bob LearnedCommented:
That would be slightly different:

button1.GetType().GetEvents()?

What do you to know from the EventInfo?
0
 
Mi-JackAuthor Commented:
Looks like I'm getting EventHandlerType, but not an EventHandler handler, so I could remove an event, like this:

button1.Click -= new EventHandler(<EventHandler >);
0
 
Bob LearnedCommented:
Where do you need to do this?  Do you have a reference to the control, and you need to do it dynamically?
0
 
Mi-JackAuthor Commented:
The control is in a dll.
By iterating the Controls collection, I find the control I need, and I need to replace the control's Click EventHandler with my own.
0
 
Mi-JackAuthor Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.