C# 中删除控件的事件的方法类

方法一:
代码
        /// <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);
        }
}

 

posted @ 2014-12-25 10:24  bobo-bobo  阅读(6699)  评论(0编辑  收藏  举报