遍历窗体上所有控件,并清空数据,重要的在于遍历所有控件,这里写了个例子。
<span style="white-space:pre"> </span>/// <summary>
/// 遍历子控件清空-TestCLeopard
/// </summary>
/// <param name="CL"></param>
private void ClearIt(Control CL)
{
try
{
foreach (Control C in CL.Controls)
{
if (C.GetType().ToString().Contains("TextBox"))
{
((TextBox)C).Text = "";
}
else if (C.GetType().ToString().Contains("ComboBox"))
{
((ComboBox)C).SelectedIndex = -1;
}
else if (C.GetType().ToString().Contains("DateTimePicker"))
{
((DateTimePicker)C).Text = DateTime.Now.ToString();
}
else if (C.Controls.Count > 0)
{
ClearIt(C);
}
}
}
catch
{
return;
}
}
直接调用即可。
例如:
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("确定要清空吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result == DialogResult.OK)
{
ClearIt(this);
}
}
原文:https://www.cnblogs.com/lonelyxmas/p/10716590.html