Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How to reorder/swap tabpage in runtime
 

How to reorder/swap tabpage in runtime

I want to reorder/swap the tab of tabpages just like tab browser in IE8.

but the tabcontrol doesn't have this functionality.


please help.
Gardenia
Angel Moon  Wednesday, February 18, 2009 5:20 AM
Take a look at the example entitled "Reposition TabItems at runtime." on my TabControl Tips page:
http://dotnetrix.co.uk/tabcontrol.htm

Or use TabControlEx instead of the standard TabControland set AllowDrop and AllowTabDrag properties to True.
http://dotnetrix.co.uk/controls.htm

Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Thursday, February 19, 2009 6:14 PM

Hi Angel Moon,

Yes. The TabControl in .Net winform doesn't offer reorder/swap function. I have any solution which you can take.

In this solution, you need to handle the MouseDown and MouseUp event of TabControl.
When the MouseDown event fired, you should remember the TabPage which is currently selected.
When the MouseUp event fired, you should calculate which TabPage the mouse point is currently over base on the e.X value.

Here is the code sample.

publicpartialclassForm1:Form
{
privateTabPageselectedTab=newTabPage();
publicForm1()
{
InitializeComponent();
tabControl1.MouseDown+=newMouseEventHandler(tabControl1_MouseDown);
tabControl1.MouseUp+=newMouseEventHandler(tabControl1_MouseUp);
}
voidtabControl1_MouseUp(objectsender,MouseEventArgse)
{
if(e.Button==MouseButtons.Left)
{
if(e.X<60)
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(0,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
elseif(e.X>=60&&e.X<120)
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(1,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
else
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(2,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
}
}
voidtabControl1_MouseDown(objectsender,MouseEventArgse)
{
if(e.Button==MouseButtons.Left)
{
selectedTab=tabControl1.SelectedTab;
}
}
}


In this example, I have create three TabPages in the TabControl. I have exactly tested each Tab's location.

tab1: 0-60
tab2: 60-120
tab3: >120

So I use Remove and Insert method to reorder the TabPages.

This solution is not perfect. When size of the tab is not a fixed number or when you create the tabpage dynamically, it will have problem. You can take this for reference.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, February 20, 2009 3:34 AM
anyone please T_T
Gardenia
Angel Moon  Thursday, February 19, 2009 8:28 AM
Take a look at the example entitled "Reposition TabItems at runtime." on my TabControl Tips page:
http://dotnetrix.co.uk/tabcontrol.htm

Or use TabControlEx instead of the standard TabControland set AllowDrop and AllowTabDrag properties to True.
http://dotnetrix.co.uk/controls.htm

Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Thursday, February 19, 2009 6:14 PM

Hi Angel Moon,

Yes. The TabControl in .Net winform doesn't offer reorder/swap function. I have any solution which you can take.

In this solution, you need to handle the MouseDown and MouseUp event of TabControl.
When the MouseDown event fired, you should remember the TabPage which is currently selected.
When the MouseUp event fired, you should calculate which TabPage the mouse point is currently over base on the e.X value.

Here is the code sample.

publicpartialclassForm1:Form
{
privateTabPageselectedTab=newTabPage();
publicForm1()
{
InitializeComponent();
tabControl1.MouseDown+=newMouseEventHandler(tabControl1_MouseDown);
tabControl1.MouseUp+=newMouseEventHandler(tabControl1_MouseUp);
}
voidtabControl1_MouseUp(objectsender,MouseEventArgse)
{
if(e.Button==MouseButtons.Left)
{
if(e.X<60)
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(0,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
elseif(e.X>=60&&e.X<120)
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(1,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
else
{
tabControl1.TabPages.Remove(selectedTab);
tabControl1.TabPages.Insert(2,selectedTab);
tabControl1.SelectedTab=selectedTab;
}
}
}
voidtabControl1_MouseDown(objectsender,MouseEventArgse)
{
if(e.Button==MouseButtons.Left)
{
selectedTab=tabControl1.SelectedTab;
}
}
}


In this example, I have create three TabPages in the TabControl. I have exactly tested each Tab's location.

tab1: 0-60
tab2: 60-120
tab3: >120

So I use Remove and Insert method to reorder the TabPages.

This solution is not perfect. When size of the tab is not a fixed number or when you create the tabpage dynamically, it will have problem. You can take this for reference.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Friday, February 20, 2009 3:34 AM
Hi Mick,

thanx for replied.

I've browse your site, it's great. you have so many resources and stuff.

also i looked to your link in codeproject ( http://www.codeproject.com/KB/tabs/draggabletabcontrol.aspx)

and i looked the code in the articel, and byit's way to copy the old tabs tonew ordered tabsthen remove and replace theoldtabs,that's not what i want.

well my concern is, just to reorder the tabs not the pages, i don't choose to that tricky code because each of my tabs will hold a control such as listview that populated with data from datasource, so if i use that technique, so the tab will make a new draw of control and new connection to data source, and of course will re populate data too.

thanx.
Gardenia
Angel Moon  Saturday, February 21, 2009 3:42 AM
Hi Kira Qian,


Well this is the same as Mick offer


thanx anyway
Gardenia
Angel Moon  Saturday, February 21, 2009 3:57 AM
Unfortunately, the tabs will be drawn in the zorder of the tabpages, so to swap the tabs you must change the order of the tabpages. The example on my site simply changes the zorder of the tabpages rather than remove and replace them.

Perhaps you could hide the tabs of a standard tabcontrol and then use a custom toolstrip to navigate the control instead.
Mick Doherty
http://dotnetrix.co.uk
Mick Doherty  Saturday, February 21, 2009 10:47 AM

Hi Angel Moon,

When I wrote the sample, I haven't gotten the sample in Codeproject. Follow your link, I feel my sample is similar to it. The method "private TabPage GetTabPageByTab(Point pt)" on that site is better than mine. It can calculate the location of each tab.

You can combine these sample solution and to choose the best one for you. The link provided by Mick Doherty are also very good.

Sincerely,
Kira Qian


Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, February 23, 2009 2:16 AM

You can use google to search for other answers

Custom Search

More Threads

• Always on top
• Cross - Threading in .Net 2005
• windows form + vb.net question...
• Another Failed to create component 'AxHost' Error
• How to add an existing object to the design surface
• Problem when switch between tabs (MDI parent)
• Scrolling textbox
• ToolStripRenderer
• The RootDesigner Can't work with generic
• Mouse Wheel Scroll C#