Click here to Skip to main content
Click here to Skip to main content

A highly configurable MDI tab control from scratch

By , 4 Jul 2011
 

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.

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

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

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.

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.

' 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)

About the Author

Eduardo Oliveira
Web Developer
Canada Canada
Member
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/Sybase, developing Client/Server applications and ASP.NET server controls for CMS.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHelp me, no change BackColor or BackgroundImage???memberkhoinguyenit11 Apr '13 - 3:19 
QuestionCould you help me to understand this codingmemberAmila thennakoon10 Mar '13 - 20:17 
QuestionMditabcontrolmemberdagronzzan7 Oct '12 - 2:04 
AnswerRe: MditabcontrolmemberTheCrazyTim21 Feb '13 - 15:02 
GeneralRe: Mditabcontrolmemberdagronzzan1 Apr '13 - 6:48 
Questionhow to make TabPage into TabGlassGradientmembersky391330 Aug '12 - 23:30 
Bughard bug!memberspiritdead13 Aug '12 - 19:51 
GeneralRe: hard bug!memberOlivier Levrey13 Aug '12 - 21:39 
BugRe: hard bug! [modified]memberspiritdead14 Aug '12 - 12:16 
THX MAN! work! finally i can work good in the designer mode Big Grin | :-D
plz add this fix in the next version
 
i see a second bug respect for use MDItabpage in tabcontrol.tabpage
 
if i use mytabcontrol.closebuttonvisible = true
 
i dont see the button close, and the button (the tabpages)

modified 14 Aug '12 - 18:27.

GeneralRe: hard bug!memberOlivier Levrey16 Aug '12 - 2:24 
GeneralRe: hard bug!memberspiritdead16 Aug '12 - 10:41 
Bugi cant see this functions ....memberspiritdead13 Aug '12 - 16:29 
GeneralRe: i cant see this functions ....memberOlivier Levrey13 Aug '12 - 21:47 
GeneralRe: i cant see this functions ....memberspiritdead14 Aug '12 - 12:36 
GeneralRe: i cant see this functions ....memberOlivier Levrey16 Aug '12 - 2:16 
Answerbug find, and fix :)memberspiritdead13 Aug '12 - 15:17 
BugGreat job. A few Dispose are just missing.memberOlivier Levrey27 Jul '12 - 2:39 
GeneralRe: Great job. A few Dispose are just missing.memberspiritdead13 Aug '12 - 15:24 
GeneralRe: Great job. A few Dispose are just missing.memberOlivier Levrey13 Aug '12 - 21:58 
QuestionFlash ProblemmemberMember 440442424 May '12 - 21:32 
QuestionAccessing dynamically created controls from another tabmemberrichardsonjoshua228@hotmail.com5 May '12 - 0:30 
Bug[Solved]Resizing problems... [modified]membertresi17 Apr '12 - 1:50 
GeneralRe: Resizing problems...membertresi17 Apr '12 - 2:45 
GeneralRe: [Solved]Resizing problems...memberJamie Balfour1 Jun '12 - 8:36 
GeneralMy vote of 5memberMember 865992528 Mar '12 - 15:17 
QuestionREORDERING TABS PROGRAMMATICALYmemberMember 774336322 Mar '12 - 7:28 
Questionadding components to the tabs?memberMember 860661730 Jan '12 - 3:07 
QuestionOpen child Form in Form (selected tab).memberNickCNS30 Dec '11 - 11:30 
QuestionProblemmemberRobertBurlile1623 Oct '11 - 12:20 
AnswerRe: Problem [modified]memberpolien25 Nov '11 - 6:42 
QuestionMDI Child Form resizememberPL0112 Sep '11 - 6:14 
AnswerRe: MDI Child Form resizememberventiventi8 Oct '11 - 14:07 
QuestionDisplay DPImemberMember 810595727 Jul '11 - 12:06 
QuestionUpdate tab tooltips?memberTim Reece25 Jul '11 - 3:36 
AnswerRe: Update tab tooltips?memberthekman2825 Jul '11 - 3:56 
AnswerRe: Update tab tooltips? [modified]memberTheCrazyTim21 Feb '13 - 14:58 
GeneralMy vote of 5memberHamlet Escaño4 Jul '11 - 3:29 
QuestionUpdate Link Please!memberMember 76859333 Jul '11 - 8:44 
AnswerRe: Update Link Please!memberEduardo Oliveira3 Jul '11 - 13:29 
GeneralLastest revision pleasememberbrightak8 Jun '11 - 6:05 
GeneralRe: Lastest revision pleasememberEduardo Oliveira3 Jul '11 - 13:28 
QuestionUrgent I need the C# codememberAETCoder16 May '11 - 12:15 
QuestionHow do I find out if there are 0 tabs left?memberIP_Programs5 Mar '11 - 15:55 
AnswerRe: How do I find out if there are 0 tabs left?memberJamie Balfour13 May '11 - 14:23 
GeneralRe: How do I find out if there are 0 tabs left?memberAETCoder18 May '11 - 5:17 
AnswerRe: How do I find out if there are 0 tabs left?memberTheCrazyTim21 Feb '13 - 15:34 
GeneralRe: How do I find out if there are 0 tabs left?memberIP_Programs22 Feb '13 - 10:19 
Generalresizing child formsmembercdm20072 Mar '11 - 6:56 
GeneralMy vote of 5memberzhouwwwjing11 Jan '11 - 20:55 
GeneralVeryGoodmembercaoqinghuajlhcn22 Oct '10 - 20:53 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 4 Jul 2011
Article Copyright 2006 by Eduardo Oliveira
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid