Ext: http://www.cnblogs.com/lovecherry/archive/2005/06/13/173668.html
(Forum answer bit) WinForm How to get the button click event Processing method
We sometimes want to know how many of the buttons on the page are available on an event delegate chain when we're doing an application, and here's an example.
1, first add 3 buttons, add 0, 1, 2 Click event Method, the button name is Button1,button2,button3 respectively. this. Button2. Click + + new System.EventHandler (this. button2_click);
this. Button3. Click + + new System.EventHandler (this. button3_click1);
this. Button3. Click + + new System.EventHandler (this. button3_click2);
2, the method can be arbitrarily write point code: private void Button2_Click (object sender, System.EventArgs e)
{
MessageBox.Show ("button2_click");
}
private void Button3_click1 (object sender, System.EventArgs e)
{
MessageBox.Show ("Button3_click1");
}
private void Button3_click2 (object sender, System.EventArgs e)
{
MessageBox.Show ("Button3_click2");
}
3, then put a TextBox (textbox1) to fill out the name of the button, a ListBox (ListBox1) display button Click event handling method, the last place a button, Click this button to display the TextBox1 fill in the button's Click event Processing method on the listbox1, the code is as follows: if (this. TextBox1.Text = "")
MessageBox.Show ("Fill in the ID of a button");
Else
{
Button btn = null;
foreach (Control C. Controls)
if (C.name = = this. TextBox1.Text)
BTN = (Button) C;
this. ListBox1.Items.Clear ();
PropertyInfo pi = (typeof (Button)). GetProperty ("Events", BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList Ehl = (eventhandlerlist) pi. GetValue (BTN, NULL);
FieldInfo fi = (typeof (Control)). GetField ("Eventclick", BindingFlags.Static | BindingFlags.NonPublic);
Delegate d = Ehl[fi. GetValue (null)];
if (d!= null)
{
foreach (Delegate de in d.getinvocationlist ())
this. ListBox1.Items.Add (DE. Method.name);
}
Else
{
this. LISTBOX1.ITEMS.ADD ("No Processing Method");
}
}
Don't forget using System.Reflection;
Why it's so complicated. It is because the events of these controls are encapsulated within the EventHandlerList, which is not a public member and therefore needs to be stripped out by reflection.