To: (Forum answer bit) WinForm How to get the button click event handling method

Source: Internet
Author: User
Tags reflection

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.