[WinForm] remove an event from the control and remove winform

Source: Internet
Author: User

[WinForm] remove an event from the control and remove winform

Key code:

/// <Summary> /// remove an event from the control /// </summary> /// <param name = "control"> control </param> /// <param name = "eventName"> name of the control to be removed: eventClick </param> public static void RemoveControlEvent (this Control control, string eventName) {FieldInfo _ fl = typeof (Control ). getField (eventName, BindingFlags. static | BindingFlags. nonPublic); if (_ fl! = Null) {object _ obj = _ fl. getValue (control); PropertyInfo _ pi = control. getType (). getProperty ("Events", BindingFlags. nonPublic | BindingFlags. instance); EventHandlerList _ eventlist = (EventHandlerList) _ pi. getValue (control, null); if (_ obj! = Null & _ eventlist! = Null) _ eventlist. RemoveHandler (_ obj, _ eventlist [_ obj]);}

Code:

        private void WinRemoveControlEventTest_Load(object sender, EventArgs e)        {            button1.RemoveControlEvent("EventClick");        }        private void button1_Click(object sender, EventArgs e)        {            MessageBox.Show("button1_Click");        }

C # Winform -- I want to use reflection to remove a specified event from a control.

In the code
This. textBox1.TextChanged-= new EventHandler (textbox?textchanged );

The premise is that the TextChange event delegate has been generated.
The Code already has
Void textBox1_TextChanged (object sender, EventArgs e)
{
Throw new NotImplementedException ();
} Such code ..

This should be irrelevant to reflection. Maybe the reflection I know is too simple.
In addition, you can use the GetEvents () method to obtain control events. However, I do not know how to use the events ..
 
C # Remove the winform tabcontrol Control

Add a construction with parameters.

You can modify the construction method of the user control.

New userForm (TableControl t, XXXX x) I don't know what type the parameter is...
You can find the data type of this. tabControl1.SelectedTab by yourself ....
What follows is that two member variables assign values to t and x.
Write t. remove (x) in the XX event of the user control)

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.