选择foreach中的所有文本框并进行编辑..不工作

时间:2014-12-22 17:55:02

标签: c# winforms foreach textbox try-catch

在我的winform中,我放了一个按钮,我想用作重置按钮。通过按下它,winform上所有文本框的文本应该变为空,或者换句话说textBox.text ="&#34 ;; 我正在使用foreach语句,但它没有工作,所以我添加了一个try catch来查看是否有任何Exception错误..在try体中的语句也没有执行也没有在catch体中..任何人都可以告诉我我在做什么错误?提前谢谢。

顺便说一句,我的winform有12个文本框。 4个不同的组合框中各有4个。

private void button2_Click(object sender, EventArgs e)
  {
     try
     {
        foreach (TextBox a in Controls.OfType<TextBox>())
        {
           a.Text = "";
        }
     }

     catch { MessageBox.Show("Error! Foreach failed"); }
  }

6 个答案:

答案 0 :(得分:1)

问题在于这句话:

  

顺便说一句,我的winform有12个文本框。 4个不同的组合框中各有4个。

这意味着您的表单没有那些TextBox控件作为直接子项。因此,使用Controls.OfType将不返回任何项目。您可以通过在foreach中设置断点来确认。你不会打它(除非你有其他文本框没有告诉我们)。

有很多种方法,很容易就是嵌套你的foreach's:

foreach (GroubBox g in Control.OfType<GroupBox>())
{
    foreach (TextBox a in g.Controls.OfType<TextBox>())
    {
       a.Text = "";
    }
}

或者您可以递归搜索控制树。这具有在一般情况下工作的优点。在How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

讨论

答案 1 :(得分:1)

与其他答案相同:您的代码只能获取父表单中的TextBox,而不是组框。 我认为这对于您的具体情况更简洁,它将在所有GroupBox中搜索TextBoxes。在功能上,它与BradleyDotNET的答案相同。

try
{
    foreach (TextBox a in Controls.OfType<GroupBox>().SelectMany(x => x.Controls.OfType<TextBox>()))
    {
        a.Text = "";
    }
}

catch { MessageBox.Show("Error! Foreach failed"); }

答案 2 :(得分:0)

foreach (TextBox a in this.Controls.OfType<TextBox>()) //assuming the text boxes are on the form only
{
  a.Text = string.Empty;
}

或者你可以采用不同的方式

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

使用OfType<TextBox>()的其他示例也可以在这里找到,但您必须了解其功能以及使用它。 Foreach Control in Controls

答案 3 :(得分:0)

虽然我无法告诉您,但您的父母似乎有问题。

TextBox1.Parent可能是GroupBox1。 GroupBox1.Parent可能是Form1。

所以你真正需要的是一种检查Form1.Controls中每个控件的方法,例如:

private Control CheckForTextBox()
{
   foreach(control c in Controls)
   {
      if(c.HasChildren)
      {
        CheckForTextBox(c);
      }

      else if(c is TextBox)
      {
        ((TextBox)c).Text = "";
      }  
   }
}

答案 4 :(得分:0)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void ClearAllTextboxes() 
    {
        ForeachTextbox(this, x => x.Text = "");
    }

    public static void ForeachTextbox(Control parent, Action<TextBox> action)
    {
        if (parent is TextBox)
        {
            action((TextBox)parent);
        }

        foreach (Control cntrl in parent.Controls)
        {
            ForeachTextbox(cntrl, action);
        }
    }
}

答案 5 :(得分:0)

我会使用递归 - 类似于:

private void ResetText(Control Ctrl)
{
    foreach (Control a in Ctrl.Controls)
    {
        if (typeof(TextBox) == a.GetType())
        {
            ((TextBox)(a)).Text = "";
        }
        else
        {
            ResetText(a);
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        ResetText(this);
    }
    catch 
    {   
        MessageBox.Show("Error!"); 
    }
}