0

I have a panel in my Windows form that I would like to add other controls like text boxes into it and also iterate over all elements in the panel to retrieve all the data in it.

Is this possible to do with a panel control?

I experimented with a foreach loop such as

foreach(textbox tb in panel1)
{
}

but I get an error saying panel does not have a public definition for GetEnumerator.

What would be a better control/container to use, where I can add more controls to it and eventually access all the controls within it an their data?


Update Just a heads up - I am having some problems with adding multiple text boxes in code. I create a textbox object and then add it to the panel, but only one shows up. I have read elsewhere on this site and others that adding a textbox with the same name might be causing the problem.

To solve this, I replaced the panel with a flow layout panel which works great. Hopefully this helps others.

RXC
  • 1,233
  • 5
  • 36
  • 67

5 Answers5

3

It's the correct control to use. Try iterating the Controls property of the panel object.

foreach(Control control in panel.Controls)
{
    if(control is TextBox)
    {
         TextBox textBox = control as TextBox;
         //etc.
    }
}
Plymouth223
  • 1,905
  • 1
  • 12
  • 22
2

You need to access Controls collection of the Panel, better if you do:

foreach(Textbox tb in panel1.Controls.OfType<TextBox>)

But the above would give you TextBoxes inside the panel, not inside other controls inside the panel, if you want to get get textboxes recursively then see this question

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

The following example clears all textbox in any control

void ClearTextBoxes(Control parent)
{
    foreach (Control child in parent.Controls)
    {
        TextBox textBox = child as TextBox;
        if (textBox == null)
            ClearTextBoxes(child);
        else
            textBox.Text = string.Empty;
    }
}

Then whenever you want clear. you call

ClearTextBoxes(panel1);
1

Something like this

foreach (Control c in panel1.Controls)
{
  if(c.GetType() == typeof(TextBox))
  {
     //do stuff
  }
}
crthompson
  • 15,653
  • 6
  • 58
  • 80
  • Note that in this case if there is a control that inherits from `TextBox`, but isn't a `Textbox` exactly, it won't be processed. If you would like to process such items then either use `IsAssignableFrom` or, better yet, just use `OfType` on the `Controls` collection. – Servy Sep 10 '13 at 20:30
  • That would go for `c is TextBox` as well wouldnt it? – crthompson Sep 10 '13 at 20:35
  • No, it wouldn't. If a type extends `TextBox` then `someDerivedType is TextBox` returns true. – Servy Sep 10 '13 at 20:39
1

You're missing one little thing. Try:

foreach (Control c in panel1.Controls)

And then check the control type if you have more than one type of control in it.

The reason for the error is that panel1 is an object, not a collection of objects, so you have to refer specifically to the collection of objects that panel1 contains.