【问题标题】:How do I clear all Textboxes, Comboboxes, and DateTimePickers on a WinForm in C#?如何在 C# 中清除 WinForm 上的所有文本框、组合框和 DateTimePickers?
【发布时间】:2013-02-18 19:32:58
【问题描述】:

我在 VS 2012 中为我的应用程序使用 C# 和 WinForms,我很好奇我应该使用什么样的例程来清除所有输入数据的方法,包括文本框、组合框和日期时间选择器.我用谷歌搜索并找到了一些“答案”,但似乎没有一个有效或实际上证明有帮助。

[编辑]:

我一直在研究,实际上发现了一个有用的方法,我只需要添加一些 if 就可以得到我想要的:

private void ResetFields()
    {
        foreach (Control ctrl in this.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                if (tb != null)
                {
                    tb.Text = string.Empty;
                }
            }
            else if (ctrl is ComboBox)
            {
                ComboBox dd = (ComboBox)ctrl;
                if (dd != null)
                {
                    dd.Text = string.Empty;
                    dd.SelectedIndex = -1;
                }
            }
            else if (ctrl is DateTimePicker)
            {
                DateTimePicker dtp = (DateTimePicker)ctrl;
                if (dtp != null)
                {
                    dtp.Text = DateTime.Today.ToShortDateString();
                }
            }
        }
    }

【问题讨论】:

  • 您正在寻找magically 清除表单上所有文本框等的机制?
  • 是的,我当然在寻找魔法......不是要添加到我的代码中的循环示例。我想要魔法
  • 不够好。您还必须检查孩子的所有孩子。看我的回答。
  • 我的似乎工作正常。
  • 那是因为你没有嵌套控件。如果在某个时候您决定添加一个带有文本框的面板怎么办?那么它就会失败。

标签: c# winforms combobox textbox datetimepicker


【解决方案1】:

有些东西这样:

void ClearThem(Control ctrl)
{
    ctrl.Text = "";
    foreach (Control childCtrl in ctrl.Controls) ClearThem(childCtrl);
}

然后:

ClearThem(this);

另一种选择: 创建一个派生自 Panel 的类,其中包含您需要的所有内容,并将其停靠在表单中。当您需要“刷新”时 - 只需将该面板替换为该面板的新实例即可。

【讨论】:

    【解决方案2】:

    您可以循环输入表单的所有控件并根据控件类型清除

    【讨论】:

      【解决方案3】:

      我们可以清除所有TextboxesComboboxes,但不能清除DateTimePicker

      如果你想清除DateTimePicker你必须设置属性: Format = CustomCustomFormat = " " 以及您想要在DateTimePicker 中选择日期的时间

          private void dateTimePicker1_CloseUp(object sender, EventArgs e)
          {
              dateTimePicker1.Format = DateTimePickerFormat.Short;
          }
      

      这可能是解决方案:

          public static void ClearAll(Control control)
          {
              foreach (Control c in control.Controls)
              {
                  var texbox = c as TextBox;
                  var comboBox = c as ComboBox;
                  var dateTimePicker = c as DateTimePicker;
      
                  if (texbox != null)
                      texbox.Clear();
                  if (comboBox != null)
                      comboBox.SelectedIndex = -1;
                  if (dateTimePicker != null)
                  {
                      dateTimePicker.Format = DateTimePickerFormat.Short;
                      dateTimePicker.CustomFormat = " ";
                  }
                  if (c.HasChildren)
                      ClearAll(c);
              }
          }
      

      【讨论】:

      • 考虑 dateTimePicker1.Value = DateTime.Today
      【解决方案4】:

      循环浏览您的表单控件,将它们与您的类型匹配并将其设置为 "" 或 null;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多