5.在WinForm中利用foreach()一次清空多个控件的方法:
以一个按钮的单击事件为例:在按钮的单击事件编写清空方法
private void button1_Click(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
//如果是文本框,清空文本框
if (c is TextBox)
{
c.Text = "";
}
//如果是单选按钮,清空单选按钮的选中状态
if (c is RadioButton)
{
RadioButton radbtn = c as RadioButton;
radbtn.Checked = false;
}
//如果是下拉列表框,清空下拉列表框为首个'请选择'选项
if (c is ComboBox)
{
c.Text = "请选择";
}
//如果是复选框,清空复选框的选中状态
if (c is CheckBox)
{
CheckBox ck = c as CheckBox;
ck.Checked = false;
}
}
}