比如:有一个按钮对象 ,它的Name=btnSave 现在怎么样通过字符串 "btnSave" 得到这个按钮对象呢?比如一个函数 Button   btn =  (Button)Fun("btnSave") 什么的。谢谢!

解决方案 »

  1.   

      Button btn = (Button)this.Controls.Find("Name", false)[0];
      

  2.   

    js这么实现的
    控件设置ID=btnSave
    document.getElementById("btnSave")
      

  3.   

    递归Find那个按钮,如果要明确知道在哪个容器里的话.直接找..不知道的话就递归吧
      

  4.   


    Button btn = (Button)this.Controls.Find("Button1", true)[0];
    修改为true,就会查找这个窗体上所有容器,这样可以了
      

  5.   

    Control GetControlByName(string name,Control parent)
    {
    Control ctl= (Control)parent.Controls.Find("Name", false)[0];
    if(ctl!=null)
    return ctl;
    else
    foreach(Control c in parent.Controls)
    {
    if(c.Controls.Count>0)
    ctl=GetControlByName(name,c);
    if(ctl!=null)
    return ctl
    }
    return null;
    }基本思路这样,徒手写的,有错的话自己改改
      

  6.   


    是winForm, 有什么办法吗?
      

  7.   

    foreach(Control   c   in   this.Controls)   
    {   
       if(c.GetType()==typeof(Button))
       {
          if(c.Name=="btnSave")
           {
               /.....
            }
       }
    }   
      

  8.   

    http://topic.csdn.net/u/20080804/08/7352bb4b-7d7f-4e0d-baad-fcf04180c091.html
    看看
      

  9.   


    public Button Fun(string btnName) 
    {
       Button btn;
       foreach(Control  c  in  this.Controls)  
       {  
           if(c.GetType()==typeof(Button)) 
           { 
              if(c.Name==btnName) 
              { 
                 btn = (Button)c;
                 break;
              } 
           } 
       }  
       return btn;
    }我这没环境.我也没测.你自己试试吧
      

  10.   


    搞定了,可能不是很好的方法,但是有用了。 Type type =this.GetType();
    FieldInfo field = type.GetField("ControlName", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
    if (field != null){
         object obj = field.GetValue(this); //可以转换成最终类型
    }多谢各位的帮助。