方法一: 代碼 /// <summary> /// 刪除指定控件的指定事件 /// </summary> /// <param name="control"></param> /// <param name="eventname"></param> public void ClearEvent(System.Windows.Forms.Control control, string eventname) { if (control == null) return; if (string.IsNullOrEmpty(eventname)) return; BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic; BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic; Type controlType = typeof(System.Windows.Forms.Control); PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags); EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null); FieldInfo fieldInfo = (typeof(System.Windows.Forms.Control)).GetField("Event" + eventname, mFieldFlags); Delegate d = eventHandlerList[fieldInfo.GetValue(control)]; if (d == null) return; EventInfo eventInfo = controlType.GetEvent(eventname); foreach (Delegate dx in d.GetInvocationList()) eventInfo.RemoveEventHandler(control, dx); } 調用:ClearEvent(button1,"Click");//就會清除button1對象的Click事件的全部掛接事件。 方法二,寫一個操做類,記錄添加的事件列表,刪除時從事件列表中讀取出來而後刪除. 代碼 class MyEventManager:IDisposable { EventHandlerList eventList = new EventHandlerList(); Hashtable eventObjectList = new Hashtable(); public void AddEvent(Control control, string eventname, EventHandler eventhandler) { string keystr = control.Name + eventname; if (!eventObjectList.Contains(keystr)) eventObjectList.Add(keystr, new object()); object eventObject = eventObjectList[keystr]; switch (eventname) { case "Click": control.Click += eventhandler; break; case "Enter": control.Enter += eventhandler; break; //... //這裏能夠添加更多的事件支持,這都是由於C# 不支持宏替換而採用的無奈之舉 //固然用反射也能夠,不過用反射就不必用這種方法了。 } eventList.AddHandler(eventObject, eventhandler); } public void DelEvent(Control control, string eventname) { string keystr = control.Name + eventname; object eventObject = eventObjectList[keystr]; Delegate d = eventList[eventObject]; if (d == null) return; foreach (Delegate dd in d.GetInvocationList()) { switch (eventname) { case "Click": control.Click -= (EventHandler)dd; break; case "Enter": control.Enter -= (EventHandler)dd; break; //... //這裏能夠添加更多的事件支持,這都是由於C# 不支持宏替換而採用的無奈之舉 //固然用反射也能夠,不過用反射就不必用這種方法了。 } } eventList.RemoveHandler(eventObject, d); eventObjectList.Remove(eventObject); } }