我想用遍例查出窗体内所有的botton控件。如何写?

解决方案 »

  1.   


    foreach(Control ctl in Controls)   
      {   
      ...   
        
      }
      

  2.   


     foreach(Control c in this.Controls)   
      {   
         switch(c.GetType().Name.ToUpper())   
         {   
           case "Button":   
           default:   
         }   
      }
      

  3.   


    foreach(Control ctl in Controls)   
      {   
      if(ctl is Button) //or if(ctl.GetType()==(new Button()).GetType)   
      ..   
      //其它类似      
      }
      

  4.   

          BLL.dIsplayServerList(cboServers);
                foreach (Control ctr in this.Controls)
                {
                    if (ctr is Button)
                    {                }
                }
      

  5.   

     private List<Button> getButton()
        {
           foreach (Control c in this.Controls)
            {
                List<Button> list = new List<Button>();
                if (c.GetType().ToString() == "System.Windows.Forms.Button")
                {
                    list.Add(c);
                }
            }
            return List;
        }
      

  6.   

    考虑容器控件. 若button在容器里.private void seachbutton(Control.ControlCollection controls)
            {
                foreach (Control c in controls)
                {
                    if (c is Button)
                        { .....}
                    else
                        if (c.Controls.Count > 0)
                            seachbutton(c.Controls);
                }        }执行:
     seachbutton(this.Controls);
      

  7.   

    foreach (Control ctr in this.Controls)
                {
                    if (ctr.getGetType().ToString() == "System.Windows.Forms.Button")
                    {
                        this.textBox1.Focus();
                    }
                }  请问我这样写怎么不能让textBox1获得焦点呢?
      

  8.   

    跟踪一下, 看 this.textBox1.Focus(); 有没有执行.若你的button是在一个容器里, 则this.textBox1.Focus(); 就不会执行.
      

  9.   

    我的button不在容器里。。但是跟踪了没执行这句话啊