0

In my winform I placed a button which i want to function as a reset button. By pressing it, the texts of all the textboxes on my winform should become empty or in other words textBox.text = ""; I am using the foreach statement but it wasnt working, so I added a try catch to see if there is any Exception error.. Neither does the statement in try body execute nor in the catch body.. Can anyone tell me what am I doing wrong? Thanks in advance.

Btw, my winform has 12 textboxes. 4 each in 3 different groupboxes.

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

     catch { MessageBox.Show("Error! Foreach failed"); }
  }
Ali
  • 71
  • 9

6 Answers6

1

The problem lies in this statement:

Btw, my winform has 12 textboxes. 4 each in 3 different groupboxes.

Which means your form does not have those TextBox controls as direct children. Thus, using Controls.OfType will return no items. You can confirm that by setting a breakpoint in the foreach. You won't hit it (unless there is some other text box you aren't telling us about).

There are many approaches, the easy one would be to just nest your foreach's:

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

Or you could search the control tree recursively. This has the advantage of working in the general case. It is discussed at: How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

Community
  • 1
  • 1
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
1

Same as the rest of the answers: Your code will only get TextBoxes in the parent form, not the group boxes. I think this is more concise for your specific situation, it will search all GroupBoxes for TextBoxes. Functionally, it is the same as BradleyDotNET's answer.

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

catch { MessageBox.Show("Error! Foreach failed"); }
lucrativelucas
  • 327
  • 2
  • 20
0
foreach (TextBox a in this.Controls.OfType<TextBox>()) //assuming the text boxes are on the form only
{
  a.Text = string.Empty;
}

or you could do it a different way

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

other examples of using OfType<TextBox>() can be found here as well but you would have to understand the functionality as well to use it.. Foreach Control in Controls

Community
  • 1
  • 1
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

Although I can't tell you for sure, it seems like you have a problem with your parents.

TextBox1.Parent is probably GroupBox1. GroupBox1.Parent is probably Form1.

So what you really need is a method to check each control in Form1.Controls such as:

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

      else if(c is TextBox)
      {
        ((TextBox)c).Text = "";
      }  
   }
}
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39
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);
        }
    }
}
John
  • 3,627
  • 1
  • 12
  • 13
0

I would use recursion - Something like:

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!"); 
    }
}
Anders H
  • 391
  • 4
  • 11