Privacy Policy


Best viewed with:
Mick Dohertys' .net Tips and Tricks - Tips / TabControl
Get a free giffgaff Sim
Change the colours of a Tabcontrols Header Item.

The first thing to do is to set the TabControls DrawMode to OwnerDrawFixed as shown above. Then simply add the code shown below.

  • VB Code
  • C# Code

I'll leave you the challenge of modifying the code to show an image.


A Completely OwnerDraw TabControl.
(As used by Microsoft)

A little more work involved here than in the previous case, but definately worth it. Add a new UserControl to your project and replace the code with that shown below. As you can see we have made the BackColor property of the TabControl Visible in the Designer and put a custom background behind Transparent Tabpages. This control has been updated to incorporate a modified version of the SelectedIndexChanging event in code examples by Matt Hinz and Ken Tucker. The project needs a custom designer to get the Design time painting to behave properly, but that's something for a later project. You'll find TabControlEx on my Controls page with all the work already done.

For more complete examples see the following articles on codeproject:
http://www.codeproject.com/cs/miscctrl/flattabcontrol.asp
http://www.codeproject.com/dotnet/CustomTabControl.asp

  • VB Code
  • C# Code

Modify the appearance to reflect the style you want.


Associate a ContextMenu with the TabItem headers of a Tabcontrol.

23 June -2006: Updated so that looping through tabpages is no longer required.

  • VB Code
  • C# Code


Hide and Show Tabpages in a Tabcontrol.

The Visible property has not been implemented on the Tabcontrol and there is no Insert method.
The following methods are a workaround..

  • VB Code
  • C# Code


An alternative approach to Tabpage Navigation.

As well as a customised navigation system, this option allows you to Enable/Disable Tabpages without the need to use a modified Tabcontrol.
This tip is mainly property manipulation.

For a better solution see the PanelManager control on my Custom Controls page. For a better solution see the PanelManager control on my Custom Controls page.

  • VB Code
  • C# Code

Enjoy!


Tabpage order has changed.

This is not random. The tabpages appear in their zOrder.
I do not know why the zOrder changes but it does.
The simple solution to reapplying the zOrder is, starting from the first tabitem and working your way to the last, select each tabpage in turn, in the order in which they should appear, (make sure the tabpage is selected and not the tabcontrol) and right click the tabpage. A menu will appear and you should select SendToBack. You may need to close and reopen the form designer to see the result in the IDE but it will be correct at runtime.


Reposition TabItems at runtime.

If you want the ability to drag tabs around at runtime then this code will do it for you.
Add a Tabcontrol (with the AllowDrop property set to True) and the code below to your form.
This code is based upon code submitted to CodeProject by Paul Auger. The original is here.

  • VB Code
  • C# Code

Assumes your Tabcontrol is named TabControl1.


A more complex TabDragging example.

Add the following classes to your project and make one of the following calls to your forms constructor after the InitializeComponent() call (assumes your TabControl is named TabControl1):

This will give you simple DragArrange of the tabs.
    [VB]    Dim DragTabs As new TabDragger(TabControl1)
    [C#]    TabDragger DragTabs = New TabDragger(this.tabControl1);

...and this will give you the ability to drag tabs out of the tabcontrol.
    [VB]    Dim DragTabs As New TabDragger(TabControl1, TabDragBehavior.TabDragout)
    [C#]    TabDragger DragTabs = new TabDragger(this.tabControl1, TabDragBehavior.TabDragOut);

  • VB Code
  • C# Code

Now that was a challenge... ;-)


Add Mnemonic support to Tabpages.

You must resort to drawing the tabitems yourself to get the underlined character to show.
This is a very basic ownerdraw example.
Set the forms KeyPreview property to True.
Set the Tabcontrols Drawmode property to OwnerDrawFixed and add the code below to your form.

  • VB Code
  • C# Code

Assumes your Tabcontrol is named TabControl1.


Tabcontrol using custom Tabpages.

This was one of the first projects I tackled in dotnet. The object was to have the tabpages support WindowsXP Visual Style. Adding visual style to an inherited TabPage was simple and after a few attempts I got the TabControl to accept the custom Tabpages via the Collection Editor.
Well since then I have learned a lot and I decided that I would rewrite the control. My goal this time was to get the DesignerVerbs to add the customised Tabpages as well as add an Insert verb. This turned out to be quite a challenge, since the TabControl Designer would not detect mouse clicks on any part of the control that was not a TabPage or TabItem. When I finally found the solution to this problem, as is usually the case, it was very simple to overcome. While I was at it I added an OnselectedIndexChanging event so that Tabpage changes may be cancelled, and a HotTab variable so you can check which tabitem the cursor is currently over. It would have been nice to add Mnemonic support, but that would involve taking full responsibility for painting the TabControl. If I'm going to do this then I may as well write the control from scratch.

  • VB Code
  • C# Code


Mirrored Tabcontrol.

Enable the RightToLeft display of Tabitems on your Tabcontrol.

  • VB Code
  • C# Code


Prevent users navigating TabControl via Ctrl+Tab and Ctrl+Shift+Tab.

Add the following code to your form

  • VB Code
  • C# Code


Add a SelectedIndexChanging Event.

The following class adds a SelectedIndexChanging event to a Tabcontrol. As well as dissallowing selection of a disabled tab via keyboard or mouse, you can now cancel a change of Tabpage.

  • VB Code
  • C# Code

Does not include code to draw disabled tabs.


Add a HideTabs property to turn on/off the Tabitems.

I don't know why I didn't think to try this earlier. This also fixes the problem of tabpages not being displayed correctly when Alignment is set to anything other than Top and Appearance is not Normal. I have left the Border around the edge at 4 (when HideTabs is False), but you may wish to change this for the Button Appearance.

  • VB Code
  • C# Code


Add a custom Scroller to Tabcontrol.

The following example adds a Custom TabScroller class to TabControl so that we may add a Close Button. For simplicity the TabScroller class is just 3 buttons placed on a Control but you may want something more complex. Using this same method it is also possible to just move the UpDown control to make room for the close button, but then you won't have control over the scroll position.

  • VB Code
  • C# Code


Create an Custom Drawn TabControl with Close Buttons.

The following example creates a fully ownerdraw tabcontrol as in the simple example above.
The example also corrects the issue where Ownerdrawn tabcontrols give the wrong tabsize and uses GDI to draw the tab text. We also draw a close button on each tab if the tab is selected or hot and close the tab if the close button is pressed.
I'll leave you to add a TabClosing event so that accidental closes can be caught.

  • VB Code
  • C# Code