学员:徐豫锋提问时间:2008-10-12 21:15:14
/// <summary>
/// 清空所有的文本框
/// </summary>
private void ClearTextBox()
{
foreach (Control ctl in form1.Controls)
{
TextBox txt = ctl as TextBox;
if (txt != null)
txt.Text = "";
}
}
如何可以判断ctl是不是TextBox,还有别的写法吗?总感觉这个方法不是很直观。。。。
回答人:amandag(高歌 .NET学习顾问)回答时间:2008-10-13 11:47:01
我试了一下
private void ClearTextBox()
{
TextBox txt;
foreach (Control ctl in this.Controls)
{
if (ctl is TextBox)
{
txt = (TextBox)ctl;
txt.Text = string.Empty;
}
}
}
在winform下没有问题可以成功运行,如果你是web应用程序,需要改为
private void ClearTextBox()
{
TextBox txt;
foreach (Control ctl in this.Form.Controls)
{
if (ctl is TextBox)
{
txt = (TextBox)ctl;
txt.Text = string.Empty;
}
}
}
学员对该答案的评价 谢谢,问题解决了