在Winform中清空窗体的简便方法可以这样写:
示例代码:
private void Clear()
{
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
c.Text = string.Empty;
}
if (c is CheckBox)
{
CheckBox ch = c as CheckBox;
ch.Checked = false;
}
}
}
但这样的话,若要清空该容器控件中的其它控件的内容会出现问题,导致其他控件不可清空。怎么办呢?
只需改动以上代码的一处即可:将this.Controls改为this.panel1.Controls
示例代码:
private void ReSetControl()
{
foreach (Control c in this.panel1.Controls)
{
if (c is TextBox)
{
c.Text = string.Empty;
}
if (c is CheckBox)
{
CheckBox ch = c as CheckBox;
ch.Checked = false;
}
}
}