[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)