zoukankan      html  css  js  c++  java
  • C#中删除控件的事件的方法

    需要事先引用

    using System.ComponentModel;
    using System.Reflection;       
     /// <summary>
            
    /// 删除指定控件的指定事件
            
    /// </summary>
            
    /// <param name="control"></param>
            
    /// <param name="eventname"></param>
            public void ClearEvent(System.Windows.Forms.Control control, string eventname)
            {
                if (control == nullreturn;
                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 == nullreturn;
                EventInfo eventInfo = controlType.GetEvent(eventname);

                foreach (Delegate dx in d.GetInvocationList())
                    eventInfo.RemoveEventHandler(control, dx);

            }
  • 相关阅读:
    Python Django开发遇到的坑(版本不匹配)
    Mysql安装与问题合集
    git branch -r查看不了远程所有分支
    angularJS使用$http请求下载excel表格
    遍历formData对象数据
    按需使用CryptoJS之AES加密(CFB)模式
    git之创建、删除分支
    git pull时报错:Access Denied (拒绝访问)
    angularJS监听数据变化
    Angular-ui-router入门
  • 原文地址:https://www.cnblogs.com/lujin49/p/2396224.html
Copyright © 2011-2022 走看看