zoukankan      html  css  js  c++  java
  • 如何判断某个事件已经绑定了某个事件处理程序?

     



    //为Button1绑定一个事件处理程序
    Button btn = new Button();
    btn.Click += new 
    EventHandler(button2_Click);
    //获取Button类定义的所有事件的信息
    PropertyInfo pi = 
    (typeof(Button)).GetProperty("Events", BindingFlags.Instance | 
    BindingFlags.NonPublic);
    //获取Button对象btn的事件处理程序列表
    EventHandlerList ehl = 
    (EventHandlerList)pi.GetValue(btn, 
    null);
    //获取Control类Click事件的字段信息
    FieldInfo fieldInfo = 
    (typeof(Control)).GetField("EventClick", BindingFlags.Static | 
    BindingFlags.NonPublic);
    //用获取的Click事件的字段信息,去匹配btn对象的事件处理程序列表,获取btn对象Click事件的委托对象
    //事件使用委托定义的,C#中的委托时多播委托,可以绑定多个事件处理程序,当事件发生时,这些事件处理程序被依次执行
    //因此Delegate对象,有一个GetInvocationList方法,用来获取这个委托已经绑定的所有事件处理程序
    Delegate 
    d = ehl[fieldInfo.GetValue(null)]; 


    foreach (Delegate del in d.GetInvocationList())
    {
        
    //判断一下某个事件处理程序是否已经被绑定到Click事件上
        Console.WriteLine(del.Method.Name == 
    "button1_Click");






    private void button1_Click(object sender, EventArgs e)
    {
        
    MessageBox.Show("Hello,button1_Click");



    private void button2_Click(object sender, EventArgs e)
    {
        
    MessageBox.Show("Hello,button2_Click");
    }


  • 相关阅读:
    oculus按键大全
    10 soundJs 初体验
    09 获取服务器时间
    08 基本数据类型转换
    07 如果再使用animateCC2018或者苹果系统使用animate时出现Uncaught ReferenceError: lib is not defined的错误
    AS3.0和php数据交互POST方式
    06 显示fps帧频
    05 js利用ajax向服务器发送请求并返回json值
    04 ajax执行php并传递参数
    03php拉取服务器信息并生成json
  • 原文地址:https://www.cnblogs.com/lujin49/p/2364185.html
Copyright © 2011-2022 走看看