当前位置:编程学习 > C#/ASP.NET >>

如何获取菜单中所绑定事件处理程序

ToolStripItem item = sender as ToolStripItem;

PropertyInfo propertyInfo = (typeof (ToolStripItem)).GetProperty("Events",  BindingFlags.Instance | BindingFlags.NonPublic);
EventHandlerList eventHandlerList = (EventHandlerList) propertyInfo.GetValue(item, null);
FieldInfo fieldInfo = (typeof(ToolStripItem)).GetField ("EventClick", BindingFlags.Static |  BindingFlags.NonPublic);
Delegate d = eventHandlerList[fieldInfo.GetValue(null)];
if (d != null)
{
    foreach (Delegate temp in d.GetInvocationList())
    {
//这里已经取得了所有Click事件绑定的处理程序,可以做一些事情。例如下面 的代码是撤销所有的事件注册
        item.Click -= temp as EventHandler;
    }
}

学电脑知识 尽在www.zzzyk.com 电脑知识网

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,