我的程序界面有几十个 textBox 控件,如果用户每次增加记录后我都要一个个去清空,实在是太麻烦了,有没什么命令或办法自动清空 几十个控件。谢谢!

解决方案 »

  1.   

    foreach(Control c in this.Controls)
    {
    if(c.GetType() == typeof(TextBox))
    {c.Text ="";}
    }就怎么简单的呀!!
      

  2.   

    哦,不可以这样吧。如果这样那用户如何接受 :(
    当做完一张单后电脑close一次再 new 一次 from ,这样用户是不会接受的
      

  3.   

    for( int i=0;i<Controls.Count;i++ )
    {
    if( Controls[i] is TextBox )
    Controls[i].Text = "";}
      

  4.   

    for( int i=0;i<Controls.Count;i++ )
    {
    if( Controls[i] is TextBox )
    Controls[i].Text = "";}
    同意
      

  5.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    this.FindCtrl(this);
    }
    private void FindCtrl( Control control )
    {
    foreach( Control c in control.Controls )
    {
    if(c is TextBox)
    {
    c.Text = "";
    }
    if (c.Controls != null)
    {
    FindCtrl( c );
    }
    }
    }
      

  6.   

    呵呵,hanbinghai(海宁) ( ) 信誉:100 正确:)用递归就全搞定了:)
      

  7.   

    不知道  cqucly() 和 tonybaobao(Tony宝宝) 的   .Text属性何来?  有没有亲自验证过?
      

  8.   

    @  hanbinghai(海宁)   and hbxtlhx(下着春雨的天):你们的  c.Text  是哪里来的?
      

  9.   

    那你这样用吧,改一下就可以了:private void ClearAllText()
    {
    ClearText(this);
    }private void ClearText(Control parentCtr)
    {
    if (parentCtr.Controls.Count==0)
    {
    return;
    }
    foreach (Control tmp in parentCtr.Controls)
    {
    ClearText(tmp); if (tmp is TextBox)
    {
    (tmp as TextBox).Text = "";
    }
    }
    }
      

  10.   

    foreach (Control control in this.Controls[1].Controls) {   if (control is TextBox)   {    ...    } } 再加上递归记住一定是Controls[1] 才是窗体上所有控件的集合
      

  11.   

    我刚才看的是 WebForm ,以为大家都是在WebForm中测试呢,结果发现上边的代码只有WinForm中测试才通过。
      

  12.   

    在 WebForm 中得按  hbxtlhx的方法  (c as TextBox).Text="";即可
      

  13.   

    我一直的做法是这样的(webform)'清空编辑框数据
        Public Sub ClearEdit(ByVal page As Page)
            Dim name As String
            Dim con As Control
            Dim i As Integer
            For i = 1 To page.Controls(1).Controls.Count - 1
                With page.Controls(1).Controls
                    If TypeOf page.Controls(1).Controls.Item(i) Is TextBox Then
                        CType(page.Controls(1).Controls.Item(i), TextBox).Text = ""
                    End If
                End With
            Next    End Sub
      

  14.   

    这个问题用  cqucly()  的方案就行了三。
      

  15.   

    如果我的 form 中有几个 tabControl,我该怎么办? 
    难道要用
    foreach(Control c in this.tabControl1.Controls) foreach(Control c in this.tabControl2.Controls) .....
    的方式去清空吗?
      

  16.   

    用数据绑定到一个DataTable,调用这个DataTable的Clear()方法就可以了
      

  17.   

    foreach(Control c in this.Controls)
    {
    if(c.GetType() == typeof(TextBox))
    {c.Text ="";}
    }
      

  18.   

    /*函数编写:
     * 函数作用:发现当页的HtmlForm窗体
     * 函数参数:一个ASP.NET的窗体
     * 函数返回值:HtmlForm
     * 函数说明:
     */
    protected HtmlForm FindHtmlForm(Page MyPage)
    {
    HtmlForm TempForm=new HtmlForm();
    for(int i=0;i<MyPage.Controls.Count;i++)
    {
    if (MyPage.Controls[i].ToString().IndexOf("HtmlForm")>0)
    {
    //取出含有所有可视控件(如TextBox,DropDownList)页面
    TempForm=(HtmlForm)MyPage.Controls[i];
    break;
    }
    }
    return TempForm;}
    ---------------//////////功能实现对页面中的TextBox进行清空
    HtmlForm MyFrom;
    MyFrom=FindHtmlForm(this);
    for(int i=0;i<MyFrom.Controls.Count;i++)
    {
    if(MyFrom.Controls[i].ToString().IndexOf("TextBox")>0)
    {
    ((TextBox)MyFrom.Controls[i]).Text="";
    }
    }