清除多个文本框,在C#中的一个按钮 [英] Clear multiple text boxes with a button in C#
本文介绍了清除多个文本框,在C#中的一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用.NET框架4。
在我的表单中,我有41文本框。
我曾尝试使用此代码:
私人无效ClearTextBoxes()
{
动作< Control.ControlCollection> ; FUNC = NULL;
= FUNC(控制)=>
{
如果的foreach(控制文本框)
(控制,文本框).Clear()(对照控制控制)
;
,否则
FUNC(control.Controls);
};
FUNC(控制);
}
和这个代码:
私人无效ClearTextBoxes(Control.ControlCollection CC)
{
的foreach(在CC控制CTRL)
{
文本框TB = CTRL为文本框;
如果(TB!= NULL)
tb.Text =的String.Empty;
,否则
ClearTextBoxes(ctrl.Controls);
}
}
这仍然不为我工作。
当我试图清理与此代码 TextBoxName.Text =的String.Empty;
文本框的成功清除,但一个TextBox,仍有一次40文本框。
我要如何解决这个问题?
修改
我把这个:
私人无效btnClear_Click(对象发件人, EventArgs五)
{
ClearAllText(本);
}
无效ClearAllText(控制CON)
{
的foreach(在con.Controls控制C)
{
如果(c是文本框)
((文本框)C).Clear();
,否则
ClearAllText(C);
}
}
但还是不行。
修改
我用面板和分离器。
解决方案
无效ClearAllText(控制CON)
{
的foreach(控制con.Controls C)
$ { b $ b如果(c是文本框)
((文本框)C).Clear();
,否则
ClearAllText(C);
}
}
要使用上面的代码,只需做到这一点:
ClearAllText(本);
I use .NET framework 4.
In my form, I have 41 text boxes.
I have tried with this code:
private void ClearTextBoxes()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
And this code:
private void ClearTextBoxes(Control.ControlCollection cc)
{
foreach (Control ctrl in cc)
{
TextBox tb = ctrl as TextBox;
if (tb != null)
tb.Text = String.Empty;
else
ClearTextBoxes(ctrl.Controls);
}
}
This still does not work for me.
When I tried to clear with this code TextBoxName.Text = String.Empty;
the textbox success cleared but one textbox, still had 40 textbox again.
How do I solve this?
EDIT
I put in this:
private void btnClear_Click(object sender, EventArgs e)
{
ClearAllText(this);
}
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else
ClearAllText(c);
}
}
but still not work.
Edit
I used panels and splitter.
解决方案
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else
ClearAllText(c);
}
}
To use the above code, simply do this:
ClearAllText(this);
这篇关于清除多个文本框,在C#中的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文