在WinForm中,能不能判断一个Button是否绑定了处理程序呢? 
如果可以,该怎么做呢?

解决方案 »

  1.   

    如果button1.Click == null,则未绑定事件处理程序。
      

  2.   

    if (button1.Click == null)
    {
    }
      

  3.   

    就是:button1.Click 是否== null
      

  4.   

    button1是封装了的
    大家都有一点想当然了吧
      

  5.   

    判定绑定事件和封不封装没啥关系吧!!!
    if (button1.Click == null)
    {
    }
    对啊
      

  6.   

    if ( button1.Click!=null)
    {}
      

  7.   

    外部应该不可以判断,不然还用private这个限制做什么,就是要保护代码安全性
      

  8.   

    如果按钮click绑定的事件方法是系统默认产生的,可以试试如下,
    如在Form1中判断Form2(或Form1本身)中的所有按钮是有事件,则
    Form2 f=new Form2();
    fe(f,f);  
    //Form1中调用时.如是判断Form1中按钮,直接fe(this,this),调试过的,代码就不格式化了private void fe(Form frm,Control pctl)
    {
    foreach(Control ctl in pctl.Controls)
    {
    if(ctl is Button)
    {
    if(HaveBindClickEvent(frm,(Button)ctl))
    this.listBox1.Items.Add(ctl.Name);
    }
    if(ctl.Controls.Count>0)
    fe(frm,ctl);
    }
    }private bool HaveBindClickEvent(Control frm,Button btn)
    {
    System.Reflection.Assembly a=System.Reflection.Assembly.GetAssembly(frm.GetType());
    System.Type typ=a.GetType(frm.GetType().FullName,true);
    System.Reflection.MethodInfo[] methods=typ.GetMethods(BindingFlags.Instance|BindingFlags.NonPublic);
    foreach(MethodInfo mi in methods)
    {
    if(mi.Name.StartsWith(btn.Name + "_Click")) //按钮事件不要乱改
    return true;
    }
    return false;
    }
      

  9.   

    public bool CheckButtonClientEvent()
    {
         return this.button1.Click != null;
    }外部调用:
    xx.CheckButtonClientEvent()