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

            }
  • 相关阅读:
    jacob根据word模板和书签插入内容,在jacob1.14.3测试成功...
    在.NET上如何根据字符串动态创建控件
    rabbitvcs一款在Linux平台下类似于乌龟的SVN客户端
    平时摘录开发经验
    C#中treeview绑定xml
    ubuntu下SVN服务器安装配置
    网页中的一些常用快捷键
    python 数据类型
    python 删除空文件夹脚本
    删除指定文件的python脚本
  • 原文地址:https://www.cnblogs.com/lujin49/p/2396224.html
Copyright © 2011-2022 走看看