C#中删除控件的事件的方法
2012-03-14 17:14
459 查看
需要事先引用
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 == 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);
}
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 == 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);
}
相关文章推荐
- C#中删除控件的事件的方法类.
- C# 001 --- 正确的删除一个控件的事件函数 --- 解决错误: “A”不包含“B”的定义,并且找不到可接受类型为“A”的第一个参数的扩展方法“B”(是否缺少 using 指令或程序集引用?)
- C# 中删除控件的事件的方法类
- C#中删除控件的事件的方法类.
- C# winform datagridview 无需点击两次即可编辑内嵌控件的方法和删除默认的空行的方法
- C#动态代码生成控件后其他事件不能获取该控件值的解决方法
- 【C#基础】之WinForm窗体常用控件的属性、方法及事件简介
- C#中combobox 控件属性、事件、方法
- C#中动态订阅控件中任意事件的方法
- Winform(C#)Flash控件 属性 方法 事件
- C# 委托之 事件 三枪控件2 2014-03-10 09:48 阅读(0) 评论(0) 编辑 删除
- asp.net(C#)中给控件添加客户端js事件的方法
- C#中删除任意控件任意事件的事件列表的实现!
- C#中combobox 控件属性、事件、方法
- c# 事件方法与窗口或控件的绑定
- 【C#基础】之WinForm窗体常用控件的属性、方法及事件简介
- 转 C#中动态订阅控件中任意事件的方法
- c# ListView控件的常用屬性、方法及事件
- asp.net(C#)中给控件添加客户端js事件的方法
- asp.net(C#)中给控件添加客户端js事件的方法