Wolfish Wolfish - 1 month ago 7
C# Question

Selecting multiple objects of the same type

I have a

TabControl
with three
TextBoxes
that must all be populated. I'm aware there is a "sort of" method of selecting multiple controls of the same type in an overall form, however I was wondering if there was a simple way to select all the
TextBoxes
within the
TabControl
. Something similar to this pseudo-code:

void checkAllBoxes(object object)
{
using ((all of type TextBox) in TabControl)
{
if object.Text.HasValue;
DoSomething();
}
}


If not, why not?

Answer

Try this:

for (int i = 0; i < tabControl1.TabCount; i++)
{
     foreach (TextBox textBox in tabControl1.TabPages[i].Controls.OfType<TextBox>().Cast<TextBox>())
     {
          textBox.Text = "";//or something else
     }
}
Comments