Click here to Skip to main content
14,980,646 members
Articles / Programming Languages / Visual Basic
Posted 12 Sep 2006


22 bookmarked

Reordering TabPages inside TabControl

Rate me:
Please Sign up or sign in to vote.
4.33/5 (12 votes)
12 Sep 2006CPOL2 min read
Extending Microsoft TabControl to enable reordering of TabPages


When using TabControl in my programs, usually there is one thing users always ask for - "let me reorder those tabs". Since reordering tabs is such a logical operation, one would think that the mighty .NET 2.0 Framework has one. But no luck there. Visual Studio 2005 does have that feature, but mortals are not blessed with it.


Visual Basic programmers waited for inheritance for far too long so they tend to use it for solving every possible problem (when you have a hammer, all problems look like nails). May I say that here, the same approach will be used.

What we want to do here is to extend the standard TabControl. Inside of it, we are interested in MouseDown, MouseMove and MouseUp events. Some people like to use drag and drop here, but that seems to me like using a bomb to kill a fly.


Upon receiving a MouseDown event, we check if the Left button is used and if TabControl has tab selected. If those requirements are fulfilled, we initialize the SourceTabPage variable.

If (e.Button = Windows.Forms.MouseButtons.Left) AndAlso _
      (MyBase.SelectedTab IsNot Nothing) AndAlso (Not MyBase.GetTabRect_
      (MyBase.SelectedIndex).IsEmpty) Then
  Me._SourceTabPage = MyBase.SelectedTab
End If

With that, we are ready for further events.


When the mouse is moved, one must check if reordering is in progress. If that is true, we look at what TabPage is under it. If it is not starting one - we have a show.

One of the things we must discover is which TabPage we are hovering over. Since unfortunately, we cannot use some sort of HitText, we must iterate through all TabPages and select one with our coordinates.

Private Function GetTabPageFromXY(ByVal x As Integer, ByVal y As Integer) As TabPage
  For i As Integer = 0 To MyBase.TabPages.Count - 1
    If MyBase.GetTabRect(i).Contains(x, y) Then
      Return MyBase.TabPages(i)
    End If
  Return Nothing

End Function

After that, we check which side we are hovering. It is only important if you wish to display a different cursor for each side. These two are the ones I use, but you can think of your own schema.

Dim currRect As Drawing.Rectangle = _
If (MyBase.TabPages.IndexOf(currTabPage) < _
      MyBase.TabPages.IndexOf(Me._SourceTabPage)) Then
  MyBase.Cursor = Cursors.PanWest
ElseIf (MyBase.TabPages.IndexOf(currTabPage) > _
      MyBase.TabPages.IndexOf(Me._SourceTabPage)) Then
  MyBase.Cursor = Cursors.PanEast
  MyBase.Cursor = Cursors.Default
End If


Here, we must know on which side we are since the new location (before or after hovering TabPage) depends on this information. This code is basically the same as the one we use for determining the cursor type.

Dim currRect As Drawing.Rectangle = _
If (MyBase.TabPages.IndexOf(currTabPage) < _
      MyBase.TabPages.IndexOf(Me._SourceTabPage)) Then 'bigger then
  MyBase.TabPages.Insert(MyBase.TabPages.IndexOf(currTabPage), Me._SourceTabPage)
  MyBase.SelectedTab = Me._SourceTabPage
ElseIf (MyBase.TabPages.IndexOf(currTabPage) > _
      MyBase.TabPages.IndexOf(Me._SourceTabPage)) Then
  MyBase.TabPages.Insert(MyBase.TabPages.IndexOf(currTabPage) + 1, Me._SourceTabPage)
  MyBase.SelectedTab = Me._SourceTabPage
End If

The only thing after that is clearing things up.

Me._SourceTabPage = Nothing
MyBase.Cursor = Cursors.Default

This is called whether we move anything or not since the user can decide to move our tab to some location where we cannot follow it.


This extended control offers a good solution to reordering TabPages inside a Tab<code>Control. I think that the only thing one may hold against it is updating the order on MouseUp but that decision is made for the sake of speed and code clarity. This way, it is very easy to implement changes through further extending since it uses protected OnMouseX procedures.

Hope you like it.


  • 12th September, 2006: Initial post


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


About the Author

Josip Medved
Software Developer (Senior) Siemens
Croatia Croatia
.NET programmer with MCSD and MCPD status. Loves chevapi...
Medo's Home Page

Comments and Discussions

QuestionCSharp version Pin
KendoTM7-Jun-12 21:36
MemberKendoTM7-Jun-12 21:36 
AnswerRe: CSharp version Pin
Josip Medved8-Jun-12 18:46
MemberJosip Medved8-Jun-12 18:46 
QuestionSmall Question Pin
Member 79877066-Jun-12 10:52
MemberMember 79877066-Jun-12 10:52 
AnswerRe: Small Question Pin
Adrao14-Jul-12 4:40
MemberAdrao14-Jul-12 4:40 
GeneralVery helpful article Pin
Anna Novikova1-Jul-10 21:55
professionalAnna Novikova1-Jul-10 21:55 
GeneralMy vote of 5 Pin
Anna Novikova1-Jul-10 21:53
professionalAnna Novikova1-Jul-10 21:53 
GeneralNice Pin
Death8U5-Feb-09 3:07
MemberDeath8U5-Feb-09 3:07 
GeneralDndTabControl functionality. Pin
MijaeDjinn1-Jul-08 10:25
MemberMijaeDjinn1-Jul-08 10:25 
JokeRe: DndTabControl functionality. Pin
Josip Medved21-Jul-08 9:50
MemberJosip Medved21-Jul-08 9:50 
GeneralDndTabControl functionality. Pin
pbnec3-Dec-08 21:35
Memberpbnec3-Dec-08 21:35 
AnswerRe: DndTabControl functionality. Pin
Josip Medved5-Dec-08 4:32
MemberJosip Medved5-Dec-08 4:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.