Key code:
// <summary> /// Remove an event from a control // </summary> // <param name= "Control" > Controls </param> /// <param name= "EventName" > Name of the control to remove eg:eventclick</param> Public Static voidRemovecontrolevent ( ThisControl control,stringEventName) {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]); } }
Using code:
private void Winremovecontroleventtest_load (object sender, EventArgs e) {button1. Removecontrolevent ( "Eventclick" ); } private void button1_click (object sender, EventArgs e) {MessageBox.Show ( "button1_click" ); }