Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms

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 2.3M   20.7K   271   313
A completely configurable tab control to use as tabbed MDI forms.

Sample Image - MDITabControl.png

Introduction

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:

VB
' 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.

VB
' replace the actual call by the new one
' MyForm.Show
TabControl1.TabPages.Add(MyForm)

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:

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

'or just use the form
TabControl1.TabPages(MyForm).Select()

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:

VB
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:

VB
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:

VB
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.

Updates

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.

VB
' 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
    g.Dispose()
    x.Dispose()
    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!

History

  • July 04, 2011: Updated download file.

License

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

 
Questionkoding form close, for msgbox Pin
Member 1020576518-Aug-13 6:51
Member 1020576518-Aug-13 6:51 
Questionadd tab button on the left Pin
Primus2x26-Jul-13 4:10
Primus2x26-Jul-13 4:10 
QuestionAtualizaçao Pin
hernany25-Jun-13 13:39
hernany25-Jun-13 13:39 
QuestionHow to change tab order Pin
Amila thennakoon21-Jun-13 1:22
Amila thennakoon21-Jun-13 1:22 
AnswerRe: How to change tab order Pin
misterff116-Nov-13 3:10
misterff116-Nov-13 3:10 
QuestionHelp me, no change BackColor or BackgroundImage??? Pin
khoinguyenit11-Apr-13 3:19
khoinguyenit11-Apr-13 3:19 
QuestionCould you help me to understand this coding Pin
Amila thennakoon10-Mar-13 20:17
Amila thennakoon10-Mar-13 20:17 
QuestionMditabcontrol Pin
dagronzzan7-Oct-12 2:04
dagronzzan7-Oct-12 2:04 
I loved it! it is amazing thank you Eduardo Oliveira!
I just have 1 question. I am making a web browser but i cant get the document title onto the tabs. i am using a geckofx webbrowser and the tabs are on top, in the title bar. Can you or anyone who is reading this please help me?
thank you,
dagronzzan
AnswerRe: Mditabcontrol Pin
TheCrazyTim21-Feb-13 15:02
TheCrazyTim21-Feb-13 15:02 
GeneralRe: Mditabcontrol Pin
dagronzzan1-Apr-13 6:48
dagronzzan1-Apr-13 6:48 
Questionhow to make TabPage into TabGlassGradient Pin
sky391330-Aug-12 23:30
sky391330-Aug-12 23:30 
Bughard bug! Pin
spiritdead13-Aug-12 19:51
spiritdead13-Aug-12 19:51 
GeneralRe: hard bug! Pin
Olivier Levrey13-Aug-12 21:39
Olivier Levrey13-Aug-12 21:39 
BugRe: hard bug! Pin
spiritdead14-Aug-12 12:16
spiritdead14-Aug-12 12:16 
GeneralRe: hard bug! Pin
Olivier Levrey16-Aug-12 2:24
Olivier Levrey16-Aug-12 2:24 
GeneralRe: hard bug! Pin
spiritdead16-Aug-12 10:41
spiritdead16-Aug-12 10:41 
Bugi cant see this functions .... Pin
spiritdead13-Aug-12 16:29
spiritdead13-Aug-12 16:29 
GeneralRe: i cant see this functions .... Pin
Olivier Levrey13-Aug-12 21:47
Olivier Levrey13-Aug-12 21:47 
GeneralRe: i cant see this functions .... Pin
spiritdead14-Aug-12 12:36
spiritdead14-Aug-12 12:36 
GeneralRe: i cant see this functions .... Pin
Olivier Levrey16-Aug-12 2:16
Olivier Levrey16-Aug-12 2:16 
Answerbug find, and fix :) Pin
spiritdead13-Aug-12 15:17
spiritdead13-Aug-12 15:17 
BugGreat job. A few Dispose are just missing. Pin
Olivier Levrey27-Jul-12 2:39
Olivier Levrey27-Jul-12 2:39 
GeneralRe: Great job. A few Dispose are just missing. Pin
spiritdead13-Aug-12 15:24
spiritdead13-Aug-12 15:24 
GeneralRe: Great job. A few Dispose are just missing. Pin
Olivier Levrey13-Aug-12 21:58
Olivier Levrey13-Aug-12 21:58 
QuestionFlash Problem Pin
cwhoedwin24-May-12 21:32
cwhoedwin24-May-12 21: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.