C# 表单中的 Foreach 控件,如何对表单中的所有文本框执行操作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1499049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:10:29  来源:igfitidea点击:

Foreach Control in form, how can I do something to all the TextBoxes in my Form?

c#winforms

提问by Sergio Tapia

How can I use a Foreach Statement to do something to my TextBoxes?

如何使用 Foreach 语句对我的文本框执行某些操作?

foreach (Control X in this.Controls)
{
    Check if the controls is a TextBox, if it is delete it's .Text letters.
}

采纳答案by JustLoren

You're looking for

您正在寻找

foreach (Control x in this.Controls)
{
  if (x is TextBox)
  {
    ((TextBox)x).Text = String.Empty;
  }
}

回答by Reed Copsey

You can do the following:

您可以执行以下操作:

foreach (Control X in this.Controls)
{
    TextBox tb = X as TextBox;
    if (tb != null)
    {
        string text = tb.Text;
        // Do something to text...
        tb.Text = string.Empty; // Clears it out...
    }
}

回答by Ian Kemp

foreach (Control X in this.Controls)
{
  if (X is TextBox)
  {
    (X as TextBox).Text = string.Empty;
  }
}

回答by JaredPar

If you are using C# 3.0 or higher you can do the following

如果您使用的是 C# 3.0 或更高版本,您可以执行以下操作

foreach ( TextBox tb in this.Controls.OfType<TextBox>()) {
  ..
}

Without C# 3.0 you can do the following

如果没有 C# 3.0,您可以执行以下操作

foreach ( Control c in this.Controls ) {
  TextBox tb = c as TextBox;
  if ( null != tb ) {
    ...
  }
}

Or even better, write OfType in C# 2.0.

或者甚至更好,用 C# 2.0 编写 OfType。

public static IEnumerable<T> OfType<T>(IEnumerable e) where T : class { 
  foreach ( object cur in e ) {
    T val = cur as T;
    if ( val != null ) {
      yield return val;
    }
  }
}

foreach ( TextBox tb in OfType<TextBox>(this.Controls)) {
  ..
}

回答by Mathlec

The trick here is that Controlsis not a List<>or IEnumerablebut a ControlCollection.

这里的诀窍是它Controls不是 aList<>IEnumerable而是 a ControlCollection

I recommend using an extension of Control that will return something more..queriyable ;)

我建议使用 Control 的扩展,它会返回更多..可查询的内容;)

public static IEnumerable<Control> All(this ControlCollection controls)
    {
        foreach (Control control in controls)
        {
            foreach (Control grandChild in control.Controls.All())
                yield return grandChild;

            yield return control;
        }
    }

Then you can do :

然后你可以这样做:

foreach(var textbox in this.Controls.All().OfType<TextBox>)
{
    // Apply logic to the textbox here
}

回答by Glenn Nassar

Just add other control types:

只需添加其他控件类型:

public static void ClearControls(Control c)
{

    foreach (Control Ctrl in c.Controls)
    {
        //Console.WriteLine(Ctrl.GetType().ToString());
        //MessageBox.Show ( (Ctrl.GetType().ToString())) ;
        switch (Ctrl.GetType().ToString())

        {
            case "System.Windows.Forms.CheckBox":
                ((CheckBox)Ctrl).Checked = false;
                break;

            case "System.Windows.Forms.TextBox":
                ((TextBox)Ctrl).Text = "";
                break;

            case "System.Windows.Forms.RichTextBox":
                ((RichTextBox)Ctrl).Text = "";
                break;

            case "System.Windows.Forms.ComboBox":
                ((ComboBox)Ctrl).SelectedIndex = -1;
                ((ComboBox)Ctrl).SelectedIndex = -1;
                break;

            case "System.Windows.Forms.MaskedTextBox":

                ((MaskedTextBox)Ctrl).Text = "";
                break;

            case "Infragistics.Win.UltraWinMaskedEdit.UltraMaskedEdit":
                ((UltraMaskedEdit)Ctrl).Text = "";
                break;

            case "Infragistics.Win.UltraWinEditors.UltraDateTimeEditor":
                DateTime dt = DateTime.Now;
                string shortDate = dt.ToShortDateString();
                ((UltraDateTimeEditor)Ctrl).Text = shortDate;
                break;

            case "System.Windows.Forms.RichTextBox":
                ((RichTextBox)Ctrl).Text = "";
                break;


            case " Infragistics.Win.UltraWinGrid.UltraCombo":
                ((UltraCombo)Ctrl).Text = "";
                break;

            case "Infragistics.Win.UltraWinEditors.UltraCurrencyEditor":
                ((UltraCurrencyEditor)Ctrl).Value = 0.0m;
                break;

            default:
                if (Ctrl.Controls.Count > 0)
                    ClearControls(Ctrl);
                break;

        }

    }
}

回答by sandy

foreach (Control x in this.Controls)
{
  if (x is TextBox)
  {
    ((TextBox)x).Text = String.Empty;
//instead of above line we can use 
     ***  x.resetText();
  }
}

回答by saleoteo

Check this:

检查这个:

foreach (Control x in this.Controls)
{
    if (x is TextBox)
    {
        x.Text = "";
    }
}

回答by DirtyHowi

simple using linq, change as you see fit for whatever control your dealing with.

简单使用 linq,根据您认为适合您处理的任何控件进行更改。

        private void DisableButtons()
    {
        foreach (var ctl in Controls.OfType<Button>())
        {
            ctl.Enabled = false;
        }
    }

    private void EnableButtons()
    {
        foreach (var ctl in Controls.OfType<Button>())
        {
            ctl.Enabled = true;
        }
    }

回答by Eduardo Quiroz Cosme

Even better, you can encapsule this to clear any type of controls you want in one method, like this:

更好的是,您可以封装它以在一种方法中清除您想要的任何类型的控件,如下所示:

public static void EstadoControles<T>(object control, bool estado, bool limpiar = false) where T : Control
        {
            foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Enabled = estado;
            foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.Enabled = estado;

            if (!limpiar) return;
            {
                foreach (var textEdits in ((T)control).Controls.OfType<TextEdit>()) textEdits.Text = string.Empty;
                foreach (var textLookUpEdits in ((T)control).Controls.OfType<LookUpEdit>()) textLookUpEdits.EditValue = @"-1";
            }
        }