Wolfish
Wolfish

Reputation: 970

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?

Upvotes: 0

Views: 257

Answers (2)

Salah Akbari
Salah Akbari

Reputation: 39966

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
     }
}

Upvotes: 3

Volkan Paksoy
Volkan Paksoy

Reputation: 6967

You can filter the textboxes inside a tab page like this:

var txtList =  tabControl1.TabPages[0].Controls.Cast<Control>()
    .Where(c => c is TextBox)
    .ToList();

Upvotes: 1

Related Questions