我是靠谱客的博主 从容故事,这篇文章主要介绍如何判断控件的事件是否存在,现在分享给大家,希望可以做个参考。

使用示例

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

最后

以上就是从容故事最近收集整理的关于如何判断控件的事件是否存在的全部内容,更多相关如何判断控件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部