INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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!

*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.

Jobs

Add tab from collection to tab control

Add tab from collection to tab control

(OP)
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:

Tabs.TabPages.Add(this.tabSelect);

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?

Thanks,

Tom

RE: Add tab from collection to tab control

(OP)
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:

CODE

Tabs.TabPages.Remove(this.tabColumns);
Tabs.TabPages.Remove(this.tabParameters);
Tabs.TabPages.Remove(this.tabSelect);
Tabs.TabPages.Remove(this.tabUpdate);
Tabs.TabPages.Remove(this.tabInsert);
Tabs.TabPages.Remove(this.tabDelete);
Tabs.TabPages.Remove(this.tabWhere);
Tabs.TabPages.Remove(this.tabOrderBy); 

Then I do:

CODE

Tabs.TabPages.Add(this.tabSelect); 

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

CODE

foreach(TabPage tc in Tabs.TabPages)
{
    if (tc.Name == "Select")
        Tabs.TabPages.Add(tc);
} 

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

Is there a way to do this?

Thanks,

Tom

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.

CODE

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

public Form1()
{
    InitializeComponent();

    foreach (TabPage tabPage in TabControl1.TabPages)
    {
        tabPages.Add(tabPage);
    }
} 


Then your code to reconstruct the tab control...

CODE

TabControl1.TabPages.Clear();

foreach (TabPage tabPage in tabPages)
{
    if (tabPage.Text == "Select")
        TabControl1.TabPages.Add(tabPage);
} 

RE: Add tab from collection to tab control

(OP)
Worked perfect.

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

Thanks,

Tom

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

CODE

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!

Resources

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