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.

1
  • 1
    Make sure that your data structure (panel1 in this case) is able to generate an iterator.
    – JNYRanger
    Sep 10, 2013 at 20:30

5 Answers 5

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

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.

1

Something like this

foreach (Control c in panel1.Controls)
{
  if(c.GetType() == typeof(TextBox))
  {
     //do stuff
  }
}
3
  • 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, 2013 at 20:30
  • That would go for c is TextBox as well wouldnt it?
    – crthompson
    Sep 10, 2013 at 20:35
  • No, it wouldn't. If a type extends TextBox then someDerivedType is TextBox returns true.
    – Servy
    Sep 10, 2013 at 20:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.