foreach(Control c in Form.Controls)
{
    try
    {
        TextBox tb = (TextBox)c;
        tb.Text = "";
    }
    catch{}
}

解决方案 »

  1.   

    Form.Controls
    这里的Form是什么?
    如果我写this.Controls这样不行
    this.Controls里面只包括窗体里面的GroupBox和TabControl
    GroupBox和TabControl里面的TextBox取不到
      

  2.   

    这样可能更安全一些:foreach(Control c in Form.Controls)
    {
    if ( c is TextBox);
    {
    c.Text = "";
    }
    //或写成一个递归
    }
      

  3.   

    这样可能更安全一些:foreach(Control c in this.Controls)
    {
    if ( c is TextBox);
    {
    c.Text = "";
    }
    //或写成一个递归
    }
      

  4.   

    一个完整点代码,可以参考下:private void button1_Click(object sender, System.EventArgs e)
    {
    foreach (Control tmpCtr in this.Controls)
    {
    if (tmpCtr is TextBox)
    {
    (tmpCtr as TextBox).Text = "";
    }
    else
    {
    ClareText(tmpCtr);
    }
    }
    }
    private void ClareText(Control pControl)
    {
    foreach (Control tmpCtr in pControl.Controls)
    {
    if (tmpCtr is TextBox)
    {
    (tmpCtr as TextBox).Text = "";
    }
    else
    {
    ClareText(tmpCtr);
    }
    }
    }
      

  5.   

    递归遍历windows窗体中的Controls容器,使用 as 判断是哪一类控件。