Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add tab from collection to tab control 1

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
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
 
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.

 
I did something similar in VB a few years ago. I'll try to find that code tonight and post tomorrow.
 
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);
}
 
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
 
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))
{
    ...
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top