views:

494

answers:

3

Hi Geeks,

Don't know if i missed a minor property or method, or if its not possible at all, how do i programmatically re order the tabs in a tab control. i need to sort the tabs depending on some conditions.

if its possible to do the reordering through the designer i guess we must be able to do it thorugh code at runtime too??/

thanks,

+3  A: 

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

thelost
redefine ? can you elaborate a bit ?? is it like i have to remove all the tabs available and then add them in the order i want ?
Aneef
You might have seen something like: tabControl.Controls.Add(tabPage); You have to simply change the order of the tab pages in the ControllCollection (like you would do with any other collection).
thelost
well this is what i did , as i said on my previous comment remove them and reorder as i wanted, i thought we could do some sorting or smt *sigh* :P, thanks anyway
Aneef
You can do sorting as well. You only have to implement a comparer method for the ControllCollection.
thelost
thanks ;) sorting idea is a good one will try that :)
Aneef
+3  A: 
  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
    • Me.TabControl1.TabPages.Remove(Me.TabPage2)
    • Me.TabControl1.TabPages.Insert(0, Me.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.

AMissico
+3  A: 

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.

Shoban