开发者

Programmatically change the tab order

How do I programmatically reorder the tabs in a TabControl? I need to sort the tabs depending开发者_JAVA技巧 on some conditions.

If it's possible to do the reordering through the designer, i guess we must be able to do it through code at runtime too.


  1. Create a new Form.
  2. Create a new TabControl.
  3. Notice it has two TabPage controls, and TabPage1 is the first tab.
  4. In form's Load event, add
    • this.TabControl1.TabPages.Remove(this.TabPage2)
    • this.TabControl1.TabPages.Insert(0, this.TabPage2)
  5. Run the form.
  6. Notice TabPage2 is now the first tab.

Note that if you fail to remove the tab page, it will still show at its old location. In other words, you will have two tabs of the same tab page.


You have to redefine your tab page collection, in order to change the index of your tab pages.


The following lines of code can also do the trick, this kind of solution also works for other kind of data that has no direct way of sorting: - convert to a list - sort the list - put it back

public static void Sort(TabControl tabControl)
{
    var tabList = tabControl.TabPages.Cast<TabPage>().ToList();
    tabList.Sort(new TabPageComparer());
    tabControl.TabPages.Clear();
    tabControl.TabPages.AddRange(tabList.ToArray());
}

public class TabPageComparer : IComparer<TabPage>
{
    public int Compare(TabPage x, TabPage y)
    {
        return string.Compare(x.Text, y.Text);
    }
}


thelost is right. Below is a quick sample code.

I have a tab control with 2 tabs (tabpage1, tabpag2)

Then I declare two tabpages and store the existing tabs in the tabcontrol in it.

abPage tbp1 = new TabPage();
TabPage tbp2 = new TabPage();

tbp1 = tabControl1.TabPages[0];
tbp2 = tabControl1.TabPages[1];

Then on a button click I removed the tabs using

tabControl1.TabPages.Remove(tabControl1.TabPages[0]);

Now if you want to change the order then you will have top add it to the tab in that order

//Order changed    
tabControl1.TabPages.Add(tbp2);
tabControl1.TabPages.Add(tbp1);

Note: This is untested quick code.


Go inside Designer.cs file

There you will find

/// [Your TabControl Name]
yourTabControl.Controls.Add(yourPage1);
yourTabControl.Controls.Add(yourPage2);
yourTabControl.Controls.Add(yourPage3);

The adding order is your tabpages' order in the tabcontrol. Change the order how you wish. Remove and Add functions of TabControl.Controls will help you as Shoban answered.


try this after Initilizacomponent(). this code will give you freedom to change it programmatically in .cs file.

        this.tabReceive.Controls.Remove(this.metroTabPage4);
        this.tabReceive.Controls.Remove(this.metroTabPage5);

        this.tabReceive.Controls.Add(this.metroTabPage4);
        this.tabReceive.Controls.Add(this.metroTabPage5);


Sometimes I have tabControls with several tabPages. At runtime tabPages are made invisible (by removing them) an added later again.

After this the tabPages may be in wrong order. I use this code to redorder them again:

public void ReorderTabPages()
{
    // Demo code to reorder tabControl with tabPages
    // where some tabPages may be unwanted at the moment

    // caution: events like "SelectedIndexChanged" does fire!

    // List of wanted tab pages
    List<TabPage> wantedTabPages = new List<TabPage>();

    // remember the current selected tab
    TabPage currentTabPage = this.tabControl.SelectedTab;

    // check if all possibly active tabs are currently visible
    // check it in the order they should be displayed
    // after that they are in the correct order in "wantedTabPages"
    if (this.tabControl.TabPages.IndexOf(this.tabPage_01) >= 0)
        wantedTabPages.Add(this.tabPage_01);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_02) >= 0)
        wantedTabPages.Add(this.tabPage_02);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_03) >= 0)
        wantedTabPages.Add(this.tabPage_03);
    if (this.tabControl.TabPages.IndexOf(this.tabPage_04) >= 0)
        wantedTabPages.Add(this.tabPage_04);

    this.tabControl.SuspendLayout();

    // remove all currently visible tab pages
    for (int i = this.tabControl.TabPages.Count - 1; i >= 0; i--)
        this.tabControl.TabPages.RemoveAt(i);

    // add the tabPages in the correct order
    foreach (var wantedPage in wantedTabPages)
        this.tabControl.TabPages.Add(wantedPage);

    // restore the currently selected tabPage
    this.tabControl.SelectedTab = currentTabPage;

    this.tabControl.ResumeLayout();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜