我们如何清除winform上的所有形式控件? [英] how can we clear the all form controls on winform?
本文介绍了我们如何清除winform上的所有形式控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想清除所有控件特别是textbox nad组合框。
和我使用以下控件清除所有字段。
I want to clear all controls specially textbox nad combobox. and I am using the following control to clear all fields.
private void ResetFields()
{
foreach (Control ctrl in this.Controls)
{
if (ctrl is TextBox)
{
TextBox tb = (TextBox)ctrl;
if (tb != null)
{
tb.Text = string.Empty;
}
}
else if (ctrl is ComboBox)
{
ComboBox dd = (ComboBox)ctrl;
if (dd != null)
{
dd.Text = string.Empty;
dd.SelectedIndex = -1;
}
}
}
}
以上代码在组框中无法正常工作。在组框中我有组合框和文本框。组合框显示组框的所选索引= 1。我也想要清除这些控件。
任何建议
The above code is not working properly in group box. In group box I have combo box and text box as well. Combo box shows the selected index = 1 of the group box. I also want to clear these controls as well. Any suggestions ????
推荐答案
TextBox
和 ComboBox
public static void ClearSpace(Control control)
{
foreach (Control c in control.Controls)
{
var textBox = c as TextBox;
var comboBox = c as ComboBox;
if (textBox != null)
(textBox).Clear();
if (comboBox != null)
comboBox.SelectedIndex = -1;
if (c.HasChildren)
ClearSpace(c);
}
}
用法: p>
Usage:
ClearSpace(this); //Control
这篇关于我们如何清除winform上的所有形式控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文