Click here to Skip to main content
15,614,943 members
Articles / Desktop Programming / Windows Forms
Posted 18 Nov 2006


271 bookmarked

A highly configurable MDI tab control from scratch

Rate me:
Please Sign up or sign in to vote.
4.87/5 (116 votes)
4 Jul 2011CPOL5 min read
A completely configurable tab control to use as tabbed MDI forms.

Sample Image - MDITabControl.png


Instead of fighting with the Forms.TabControl, I thought that writing one from scratch should be a lot of fun. My goal was to produce a tab control with specific behaviours, and simple to use, and at the same time, with explorer inheritance, graphics, delegation, and collections.

This tab control is specific to creating a MDI interface with one tab page for each form. Some of the behaviours that were implemented are:

  • Always open the new page at the first tab
  • Hide the tab pages that can't fit in the screen
  • Don't scroll tab pages, instead, bring a hidden tab page to the first position when it's selected
  • Have a list of the tab pages (forms) in a drop down menu
  • Ability to reorder tabs by dragging
  • Easy owner draw
  • Close button at the tab
  • Clicking on overlapped part of the tab does not select the other tab and also
  • Clicking outside the border doesn't select the tab (achieved by the Region property)
  • Expose the maximum of functionality and configurability as possible
  • Very simple to use

What this is not

Well, this is not a docking panel. Also, there is some functionality that wasn't implemented like left/right Alignment, RightToLeft, support to add tab pages at design time (what doesn't really make sense in this case), container tab pages, and others.

This is not a very fast control, since I exposed a lot of functionality/customizations, but did my best to be OK and look cool.

What can be customized

Almost everything. To understand the control, let's take a look at the objects and regions.

Image 2

  • The first two lines are the TopSeparator
  • Followed by the TabTop
  • On the left the Tabs area
  • On the right the Control Buttons area
  • The space between the tabs is the TabOffset
  • All the bottom part is the form

The tab is composed of the following areas from left to right:

  • TabPadLeft
  • Icon
  • Text
  • CloseButton
  • TabPadRight

Most customizations are made through properties. All the properties are in the TabControl, and some of them are in the TabPage, so you can change some appearance on each specific tab.

The icon and the text in the tab are the Icon and Text properties of the form, respectively. All the colors in the control can be changed. The close button in the tab can be replaced by an image. The TopSeparator, the tab icon, the tab Close button, and the control buttons can be displayed or not. And there is a lot more. Look at the control to see what’s possible.

The tab shape can be changed by the GetTabRegion event. The tab background can be OwnerDraw by the event TabPaintBackGround, and the border can be handled by the event TabPaintBorder. The events occur in the order mentioned.

Using the control

To use the control, you first need to add it to a form. Ideally, you should dock the control in it.

After that, you can change all the properties through the designer.

To insert a TabPage (form) in the control:

' Create the form
Dim MyForm As New AnyForm

' Insert the provided form in the TabControl1
' and save the TabPage in the Form.Tag property
MyForm.Tag = TabControl1.TabPages.Add(MyForm)

You don't show the form, instead you just add it to the control. Very simple, isn't it? On an existing program, you just need to add the control to the main form, and where you have the Show call for the form, you just replace by the Add method.

' replace the actual call by the new one
' MyForm.Show

You will probably not need to change any property in your form. The Add method changes the necessary ones.

TabControl.TabPages.Add is a function that returns the created TabPage. You can save it to a variable, or you can ignore the returned value. In the example above, I saved the TabPage in the Form.Tag property, this is a good Idea if you want to have a fast access to the TabPage.

To activate an existing form in the control:

' You can use the TabPage
DirectCast(MyForm.Tag, MdiTabControl.TabPage).Select()

'or just use the form

You can access a tab page by the Index or by the Form, and you can get the Index by the TabPage or by the Form.

To change the tab shape:

Private Sub TabControl2_GetTabRegion(ByVal sender As Object, ByVal e As _
        MdiTabControl.TabControl.GetTabRegionEventArgs) _
        Handles TabControl2.GetTabRegion
    ' you can create a new point array or just modify the existing one
    e.Points(1) = New Point(e.TabHeight - 2, 2)
    e.Points(2) = New Point(e.TabHeight + 2, 0)
End Sub

This example above changes the default shape to the colourful one in the first picture.

To OwnerDraw the tab background:

Private Sub TabControl1_TabPaintBackground(ByVal sender As Object, ByVal e As _
        MdiTabControl.TabControl.TabPaintEventArgs) _
        Handles TabControl1.TabPaintBackground
    ' Say to the control to don't paint the background
    e.Handled = True

    ' here add your code to paint the background
    ' the TabPaintEventArgs has several properties to help

End Sub

To OwnerDraw the tab border:

Private Sub TabControl1_TabPaintBorder(ByVal sender As Object, ByVal e As _
        MdiTabControl.TabControl.TabPaintEventArgs) _
        Handles TabControl1.TabPaintBorder

' Say to the control to don't paint the border
    e.Handled = True

    ' here add your code to paint the background
    ' the TabPaintEventArgs has several properties to help

End Sub

You can leave the Handled property set to false so the control will draw the border, but you can do some extra painting after the control has painted the background and before the border painting, as you can see in the "MY MDI Form #2" tab in the first picture.

Inside the code

The class TabPage inherits from System.Windows.Forms.Control. The control itself is the tab, and the form is an attribute of it. It exposes the Click event, which is fired when the tab is clicked/selected. There is no CloseClick event, since it is captured by the form Close event.

The class TabControl inherits from System.Windows.Forms.UserControl. This class has three internal classes. The GetTabRegionEventArgs is inherited from System.EventArgs, the TabPaintEventArgs inherited from PaintEventArgs, and the TabPageCollection inherited from CollectionBase.

Also, there is the Friend class ControlButton inherited from System.Windows.Forms.Control that is used by the DropButton and the global CloseButton.

The Close button in the tab is not a contained control. It's part of the control itself and is controlled by the tab's mouse events.


Some enhancements and some changes to the original version. The file LastChanges.txt on the zip file has what's changed.

The new property TabGlassGradient deserves a comment here. My solution for the Glass Look that's on Firefox tabs and IE 7 tabs was pretty simple. I get the gradient from two colors, cut the gradient in 1/3, and invert the bottom. I don't know how the others are doing it, but works fine this way. Here is the code.

' returns a new gradient brush with the glass look
Friend Function CreateGlassGradientBrush( _
       ByVal Rectangle As Rectangle, ByVal Color1 As Color, _
       ByVal Color2 As Color) As Drawing2D.LinearGradientBrush
    ' creates a new gradient brush
    Dim b As New Drawing2D.LinearGradientBrush(Rectangle, _
    Color1, Color2, Drawing2D.LinearGradientMode.Vertical)
    ' creates a new bitmap to to render the gradient
    Dim x As New Bitmap(1, Rectangle.Height)
    Dim g As Graphics = Graphics.FromImage(x)
    ' paint the gradient on the bitmap
    g.FillRectangle(b, New Rectangle(0, 0, 1, Rectangle.Height))
    ' create a new color blend with 4 colors and copy the
    ' colors from the bitmat 
    Dim c As New Drawing2D.ColorBlend(4)
    c.Colors(0) = x.GetPixel(0, 0)
    c.Colors(1) = x.GetPixel(0, x.Height / 3)
    ' the trick is here the 3rd color is the last color in
    ' the bitmap
    c.Colors(2) = x.GetPixel(0, x.Height - 1)
    ' and the last color is the one at 1/3
    c.Colors(3) = x.GetPixel(0, x.Height / 3)
    c.Positions(0) = 0
    c.Positions(1) = 0.335
    c.Positions(2) = 0.335
    c.Positions(3) = 1
    ' assign the color blend to the brush
    b.InterpolationColors = c
    Return b
End Function

Don't forget to recompile before opening the main form on the test project.

That's it for now. I'm gonna have a diet Pepsi!


  • July 04, 2011: Updated download file.


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

Written By
Software Developer (Senior)
Canada Canada
Eduardo Oliveira graduated in Computer Systems Analysis in Rio de Janeiro - Brazil in 1990.
He has been working as Programmer Analyst since.
In 2001 immigrated to Canada and today lives in Calgary and works with .NET and SQL server, developing desktop and web applications.

Comments and Discussions

QuestionDropdown tab selection doesn't work after the tab control has is resized Pin
blkwebman14-Mar-18 6:13
blkwebman14-Mar-18 6:13 
QuestionResizing of multiple tabs??? Pin
blkwebman5-Mar-18 4:02
blkwebman5-Mar-18 4:02 
QuestionDetach form Pin
digitalturbulence2-Feb-18 11:48
digitalturbulence2-Feb-18 11:48 
QuestionSelect C # tab Pin
Member 1238588111-Apr-17 11:22
Member 1238588111-Apr-17 11:22 
Questionplease as soon as u can Pin
Member 1083960424-Feb-17 22:50
Member 1083960424-Feb-17 22:50 
QuestionHow to animate icon of particular tabpage Pin
Member 124709967-Jan-17 22:45
Member 124709967-Jan-17 22:45 
QuestionMDIForm ToolStrip Control Connected with all child form Pin
riswanown31-Oct-16 0:13
riswanown31-Oct-16 0:13 
QuestionDatagridview Problem Pin
Member 476126219-May-15 6:53
Member 476126219-May-15 6:53 
AnswerRe: Datagridview Problem Pin
Member 476126219-May-15 9:43
Member 476126219-May-15 9:43 
QuestionOpen tab from another form Pin
Capsulecorp12-May-15 6:44
Capsulecorp12-May-15 6:44 
AnswerRe: Open tab from another form Pin
Member 476126220-Jul-15 2:48
Member 476126220-Jul-15 2:48 
Questionchange the opening direction Pin
kalicem11-Mar-15 19:45
kalicem11-Mar-15 19:45 
AnswerRe: change the opening direction Pin
kalicem12-Mar-15 2:31
kalicem12-Mar-15 2:31 
GeneralRe: change the opening direction Pin
Antonio_cadiz19-Mar-15 3:32
Antonio_cadiz19-Mar-15 3:32 
GeneralRe: change the opening direction Pin
kalicem19-Mar-15 4:38
kalicem19-Mar-15 4:38 
GeneralRe: change the opening direction Pin
Ahmed Aboelez21-Nov-15 0:05
Ahmed Aboelez21-Nov-15 0:05 
QuestionHow to activate already selected tab? Pin
susindharan27-Jan-15 19:03
susindharan27-Jan-15 19:03 
SuggestionNew Tab Button Pin
Member 1085700124-Oct-14 20:57
Member 1085700124-Oct-14 20:57 
GeneralRe: New Tab Button Pin
kalicem12-Mar-15 10:08
kalicem12-Mar-15 10:08 
AnswerRe: New Tab Button Pin
JTechMe20-Jun-16 12:15
JTechMe20-Jun-16 12:15 
QuestionHow to add a scroll button to scroll next or back like a browser ? Pin
Member 1023402519-Jul-14 1:37
Member 1023402519-Jul-14 1:37 
QuestionResizing problem... when main form (included MDI tab control) was resized Pin
Seongkyu LEE16-May-14 20:31
Seongkyu LEE16-May-14 20:31 
Answer[Solved] Re: Resizing problem... when main form (included MDI tab control) was resized Pin
Seongkyu LEE16-May-14 22:32
Seongkyu LEE16-May-14 22:32 
GeneralRe: [Solved] Re: Resizing problem... when main form (included MDI tab control) was resized Pin
Ahmed Aboelez29-Nov-15 7:58
Ahmed Aboelez29-Nov-15 7:58 
QuestionHow to make program close when last tab is closed Pin
Member 1074291413-Apr-14 7:17
Member 1074291413-Apr-14 7:17 

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.