重置表单中的所有项目 [英] Reset all the items in a form

查看:138
本文介绍了重置表单中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是有办法,我可以将所有的复选框,文本框,数字和其他控件恢复为默认值,而无需单独为每个控件编写代码?这是我试过的代码,但似乎并没有工作:

I was wondering, is there a way I can reset all the checkboxes, textboxes, numerics and other controls back to the default values without writing code for every control individually? This is the code I've tried, but doesn't seem to work:

for (int i = 0; i < this.Controls.Count; i++)
{
    this.Controls[i].ResetText();
}



编辑:结果
我已经通过手动固定它设置控制值,对不起,所有的烦恼><


I've fixed it by manually setting the control values, sorry for all the trouble >.<.

推荐答案

做如下创建类并调用它像这样

Do as below create class and call it like this

检查:重置所有控件(文本框,组合框,复选框,列表框)在Windows窗体中使用C#

private void button1_Click(object sender, EventArgs e)
{
   Utilities.ResetAllControls(this);
}







public class Utilities
    {
        public static void ResetAllControls(Control form)
        {
            foreach (Control control in form.Controls)
            {
                if (control is TextBox)
                {
                    TextBox textBox = (TextBox)control;
                    textBox.Text = null;
                }

                if (control is ComboBox)
                {
                    ComboBox comboBox = (ComboBox)control;
                    if (comboBox.Items.Count > 0)
                        comboBox.SelectedIndex = 0;
                }

                if (control is CheckBox)
                {
                    CheckBox checkBox = (CheckBox)control;
                    checkBox.Checked = false;
                }

                if (control is ListBox)
                {
                    ListBox listBox = (ListBox)control;
                    listBox.ClearSelected();
                }
            }
        }      
    }

这篇关于重置表单中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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