清除Windows窗体中的控件文本 [英] clearing the controls text in a windows form

查看:52
本文介绍了清除Windows窗体中的控件文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用foreach循环清除表单中的所有控件值

How to clear all the controls values in a form using foreach loop

推荐答案

您需要编写一个递归方法:

You need to write a recursive method:

private void ClearAllTextBoxes(Control control)
{
    foreach (Control item in control.Controls)
    {
        if (item is TextBox)
        {
            ((TextBox)item).Text = "";
        }
        else if (item.HasChildren)
        {
            ClearAllTextBoxes(item);
        }
    }
}



然后从其他地方这样调用它:



And then call it like this from somewhere else in the form:

ClearAllTextBoxes(this);


这篇关于清除Windows窗体中的控件文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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