C# winform编程,怎么遍历textbox 控件?

解决方案 »

  1.   

    foreach(Control   txt   in   Form.Controls)   
      {   
            if(txt   is   TextBox)   
            {   
                ................  
            }   
        
      }
      

  2.   

    foreach(Control   c   in   this.Controls)   
        {   
          if(c   is   TextBox)   
          {   
           ..... 
          } 
         }  
      

  3.   

    Form1()
    {
      Foo(this.Controls);
    }void Foo(Control.ControlCollection ctls)
    {
      foreach (Control ctl in ctls)  
      {  
        if (ctl is TextBox)  
        {  
          // 对 TextBox 进行操作
        }
        else if (ctl.Controls != null) Foo(ctl.Controls); // 递归遍历子控件
      }  
    }
      

  4.   

    我来写个js的 呵呵txtCount=0;
    obj=document.getElementByTagName("input")
    for (i=0;i<obj.length;i++)
    {
        if (obj.type="text")
        {
           txtCount=txtCount+1;
        }
    }
      

  5.   

    int i = 0;
    foreach (System.Windows.Forms.Control control in this.Controls)   
       {   
        if (control is System.Windows.Forms.TextBox)   
        {   
            i++;
        }   
       }  
    messagebox.show(i.tostring());
      

  6.   

    上面代码 写错了
     if   (obj.type== "text ") 
    少了个等于号
      

  7.   


    用这个,如果有容器的话也要遍历容器里面的TextBox控件
      

  8.   

     public void FindControl(Control.ControlCollection  c)
             {
                foreach (System.Windows.Forms.Control control in c)
                { 
                  if (control.HasChildren)
                  {
                      FindControl(control.Controls);
                  } 
                  else
                  {
                  if (control is System.Windows.Forms.TextBox) 
                  { 
                    System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control;               }
                  }
                }
            }
      

  9.   

      foreach (System.Windows.Forms.Control control in this.Controls)
                {
                    if (control is System.Windows.Forms.TextBox)
                    {
                       
                    }
                }
      

  10.   


      foreach (Control c in this.groupBox1.Controls)
                {
     if (c is textButton)
                    {
                       
                        ....              
                    }
    }
      

  11.   

    到网上找到一个较好答案了,多谢各位观注。
    //----------------------------------------------------
            private void OperateControls(Control control)
            {
                foreach (Control c in control.Controls)
                {
                    if (c is Panel)
                    {
                        OperateControls(c);
                    }
                    if (c is GroupBox)
                    {
                        OperateControls(c);
                    }
                    if (c is TextBox)
                    {
                        //   它是   TextBox,   要干什么随便你   
                        c.Text="";
                    }
                }
            }   
    //调用------------------------------------
      OperateControls(this);