Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Add tab from collection to tab control

Add tab from collection to tab control

Add tab from collection to tab control

Is there a way to add a current tab to the tab control by name?

I currently have 7 tabs in my tab controls collection. I remove all the tabs from the collection at the start of the program and add in the ones I want based some criteria.

But I add them like so:


But I want to be able to add them by name (or some other way dynamically).

The tabSelect tab which is in the collection has a text name of "Select".

But if I do Tabs.TabPages.Add("Select"), it creates a new page, not use the one in the collection. Is there a way to do it programmatically?



RE: Add tab from collection to tab control

I tried to remove all the tabpages that are in the collection, which is what I usually do and then add the pages back in by their name. That works fine. But I only know the text of the tab and want to add it back in that way.

I first:



Then I do:



But I want to add in a couple of tabpages from the collection based on some criteria, so I tried this:


foreach(TabPage tc in Tabs.TabPages)
    if (tc.Name == "Select")

As I mentioned, it never gets to the "if" statement.

Is there a way to do this?



But it finds nothing so it jumps over the foreach since there is nothing left in the collection. But it obviously knows of the object, since I am able to speciifically add it in.

RE: Add tab from collection to tab control

I did something similar in VB a few years ago. I'll try to find that code tonight and post tomorrow.

RE: Add tab from collection to tab control

Once you've removed all the tab pages from the tab control you can no longer loop through and add them back in. What you need to do is save all the tab pages in a list (in the constructor) and then loop through that list to add them back in.


private List<TabPage> tabPages = new List<TabPage>();

public Form1()

    foreach (TabPage tabPage in TabControl1.TabPages)

Then your code to reconstruct the tab control...



foreach (TabPage tabPage in tabPages)
    if (tabPage.Text == "Select")

RE: Add tab from collection to tab control

Worked perfect.

Also, is there a way to put them in a particular order. They seem to show up in the order added.



RE: Add tab from collection to tab control

Each TabPage has a TabIndex property which, strangely, is not available in the Properties window. If you want a particular order, why not just add them in the order you want them?

If you are able to modify each TabPage's TabIndex property (either within the Form.Designer.cs file or in the form's constructor or Load event) then you can loop through them in that specific order like so...


foreach (TabPage tabPage in _tabPages.OrderBy(tp => tp.TabIndex))

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close