Foreach Control在表单中,如何对表单中的所有TextBox进行操作?

时间:2009-09-30 15:55:37

标签: c# winforms

如何使用Foreach语句对TextBoxes执行某些操作?

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

14 个答案:

答案 0 :(得分:60)

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

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

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

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

甚至更好,在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)) {
  ..
}

答案 1 :(得分:54)

您正在寻找

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

答案 2 :(得分:8)

这里的诀窍是Controls不是List<>IEnumerable,而是ControlCollection

我建议使用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;
        }
    }

然后你可以这样做:

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

答案 3 :(得分:4)

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

答案 4 :(得分:4)

您也可以使用LINQ。例如,对于明确的Textbox文本,请执行以下操作:

this.Controls.OfType<TextBox>().ToList().ForEach(t => t.Text = string.Empty);

答案 5 :(得分:3)

您可以执行以下操作:

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...
    }
}

答案 6 :(得分:0)

只需添加其他控件类型:

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;

        }

    }
}

答案 7 :(得分:0)

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

答案 8 :(得分:0)

检查一下:

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

答案 9 :(得分:0)

简单地使用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;
        }
    }

答案 10 :(得分:0)

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

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";
            }
        }

答案 11 :(得分:0)

private IEnumerable<TextBox> GetTextBoxes(Control control)
{
    if (control is TextBox textBox)
    {
        yield return textBox;
    }

    if (control.HasChildren)
    {
        foreach (Control ctr in control.Controls)
        {
            foreach (var textbox in GetTextBoxes(ctr))
            {
                yield return textbox;
            }
        }
    }
}

答案 12 :(得分:0)

以上很多工作。 只是添加。如果您的文本框不在窗体上,而是在其他容器对象(如GroupBox)上,则必须获取GroupBox对象,然后遍历GroupBox才能访问其中包含的文本框。

foreach(Control t in this.Controls.OfType<GroupBox>())
{
   foreach (Control tt in t.Controls.OfType<TextBox>())
   {
        // do stuff
   }
}

答案 13 :(得分:0)

我发现这很好用,但是最初我在面板上放置了文本框,因此没有按预期清除所有文本框,所以我添加了

this.panel1.Controls.....  

让我想到的是,您有许多用于不同功能的文本框,只需要清除一些即可,也许使用多个面板层次结构,您可以仅定位几个而不是全部。

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