用于清除表单c#中所有文本框的代码 [英] code for clearing all textbox in a form c#

查看:128
本文介绍了用于清除表单c#中所有文本框的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于清除表格中所有文本框的代码c#

code for clearing all textbox in a form c#

推荐答案

其他解决方案没有考虑控件可以嵌套(我个人总是放一个数字面板(分离器等)并添加其他控件作为Panel的子项。)



因此,解决方案需要递归:



The other solutions do not take into account that the controls can be nested (I personally always put a number of Panels (splitters, etc.) and add other controls as Panel's children).

So, the solution needs to be recursive:

void ClearTextBoxes(Control parent) {
    foreach (Control child in parent.Controls) {
        TextBox textBox = child as TextBox;
            if (textBox == null)
                ClearTextBoxes(child);
            else
                textBox.Text = string.Empty;
    } //loop
} //ClearTextBoxes

//...

ClearTextBoxes(myForm);





< dd> -SA


尝试:

Try:
foreach (Control c in Controls)
   {
   TextBox tb = c as TextBox;
   if (tb != null)
      {
      tb.Text = string.Empty;
      }
   }


试试这个:



Try this:

foreach (TextBox t in this.Controls.OfType<TextBox>())
            {
                t.Text = string.Empty;
            }


这篇关于用于清除表单c#中所有文本框的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆