Programmatically change the tab order

Aneef picture Aneef · Apr 1, 2010 · Viewed 19.7k times · Source

How do I programmatically reorder the tabs in a TabControl? I need to sort the tabs depending 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.

Answer

AMissico picture AMissico · Apr 1, 2010
  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.