Abo*_*Kdz 4 c# textbox winforms
我使用.NET framework 4.
在我的表格中,我有41个文本框.
我尝试过这段代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
这段代码:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这仍然不适合我.
当我尝试使用此代码TextBoxName.Text = String.Empty;清除时,文本框成功清除但只有一个文本框,仍然有40个文本框.
我该如何解决这个问题?
编辑
我投入了这个:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
但仍然不行.
编辑

我使用了面板和分离器.
Use*_*384 14
void ClearAllText(Control con)
{
foreach (Control c in con.Controls)
{
if (c is TextBox)
((TextBox)c).Clear();
else
ClearAllText(c);
}
}
Run Code Online (Sandbox Code Playgroud)
要使用上面的代码,只需执行以下操作:
ClearAllText(this);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26760 次 |
| 最近记录: |