//方法1) 使用TextBox的Clear(),方法
foreach (Control control in this.Controls)
{if (control is TextBox)
{
((TextBox)control).Clear();
}
}
//方法2)使用TextBox的Text 属性
foreach (Control control in this.Controls)
{if (control is TextBox)
{
control.Text=String.Empty;
}
}
//注意事项:
如果有TextBox控件在容器里,如GroupBox,遍历时要注意,在foreach中不能使用in this.Controls,因为GroupBox里的控件不是Form里的Controls属性的Items.需要相应的改成:
foreach (Control control in this.groupBox1.Controls)
{
if (control is TextBox)
{
((TextBox)control).Clear();
}
}