Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anybody tell me how to set the Tabpage index in tabcontrol.
When i was adding a new tab to a tabcontrol and moving it to front. the indexes are not changing.

Thanks with regards,
Gani.P
Posted

The index is set automatically when you fill the TabPages property:

C#
//page1 will receive index 0
tabControl1.TabPages.Add(page1);
//page2 will receive index 1
tabControl1.TabPages.Add(page2);
//page3 will receive index 2
tabControl1.TabPages.Add(page3);


If you want to change indices, just change the add order:
C#
//page2 will receive index 0
tabControl1.TabPages.Add(page2);
//page1 will receive index 1
tabControl1.TabPages.Add(page1);
//page3 will receive index 2
tabControl1.TabPages.Add(page3);


Or as OriginalGriff said, you can use the Insert method for example.

Using BringToFront will not help there.
 
Share this answer
 
v2
Comments
OriginalGriff 2-Aug-11 9:01am    
I'm afraid that isn't true: the TabPages property is a normal List, and so you can use any IList functions which affect the index: Insert, DeleteAt, etc.
Olivier Levrey 2-Aug-11 9:15am    
I thought TabPage had a readonly Index property but I was wrong. Hence, my answer was not clear: I meant this readonly property would be set automatically...
I didn't mean the list can't be edited as you said.
Olivier Levrey 2-Aug-11 9:17am    
I corrected my answer. Thanks for that.
OriginalGriff 2-Aug-11 9:25am    
You're welcome!
Sergey Alexandrovich Kryukov 3-Aug-11 1:39am    
Good point; good answer -- after correction. My 5.
--SA
Use the TabPages.Insert method instead of Add:
C#
TabPage tb = new TabPage("Hello");
myTabControl.TabPages.Insert(0,tb);
 
Share this answer
 
Comments
Olivier Levrey 2-Aug-11 9:00am    
Good too. 5.
Aydin Homay 28-Oct-13 5:22am    
Dear OriginalGriff
Your answer is not complete because you can not add new tab page with these cods you should
read TabControl handle before than !!!!!!!!!
TabPage tb = new TabPage("Hello");
IntPtr h = tabControl1.Handle;
tabControl1.TabPages.Insert(0, tb);
Yes it is a bug in Microsoft Tab Control
Sergey Alexandrovich Kryukov 3-Aug-11 1:39am    
Correct, a 5.
--SA
OriginalGriff 28-Oct-13 5:28am    
Um. Yes you can...it works fine for me, and has done for many years...
Aydin Homay 28-Oct-13 6:09am    
No you can not!!! you should read handle of TabContorol before the inserting new TabPage please check your code ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900