如何判断控件的事件是否存在
2017-12-06 10:36
471 查看
使用示例
var isBind = IsBindEvent(comboBox.GetType(), comboBox, "comboBox_TextChanged");
if (!isBind)
{
comboBox.TextChanged += comboBox_TextChanged;
}
var isBind = IsBindEvent(comboBox.GetType(), comboBox, "comboBox_TextChanged");
if (!isBind)
{
comboBox.TextChanged += comboBox_TextChanged;
}
//是否已经绑定了事件 private bool IsBindEvent(Type type, Control con, string eventName) { bool isBind = false; PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic); //获取type类定义的所有事件的信息 EventHandlerList ehl = (EventHandlerList)pi.GetValue(con, null); //获取con对象的事件处理程序列表 FieldInfo fieldInfo = (typeof(Control)).GetField("EventText", BindingFlags.Static | BindingFlags.NonPublic); //获取Control类Click事件的字段信息 Delegate d = ehl[fieldInfo.GetValue(null)]; if (d == null) { return isBind; } foreach (Delegate del in d.GetInvocationList()) { if (del.Method.Name == eventName) { isBind = true; break; } } return isBind; }
相关文章推荐
- 如何判断事件是否存在?
- [VB.NET]如何在一个窗体M中判断另一个窗体N中是否存在一个名称为A的事件?
- 如何在JS中判断页面上是否存在指定控件
- 如何使用JQuery去判断一个元素(对象)是否存在
- 如何判断传过来的JSON数据中,某个字段是否存在
- VBA实践技巧精粹008:如何判断指定工作表是否存在
- QTP中如何判断Excel进程是否存在?
- 在jQuery,如何判断页面元素是否存在?
- 如何判断Javascript对象是否存在
- 如何判断Javascript对象是否存在
- 如何判断一个单链表中是否存在环
- 判断控件数组中的元素是否存在
- 用TXMLDocument处理xml文件时,如何判断某一结点是否存在
- 关于ODBC API编程中如何判断DSN是否已存在
- QTP中如何判断Excel进程是否存在?
- jquery和js如何判断一个对象是否存在
- python中如何判断某个变量是否存在
- 如何判断单链表是否存在环
- Go语言的map如何判断key是否存在
- Selenium2_如何判断WebElement元素对象是否存在