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

TabStrips: A TabControl in the Visual Studio 2005 way!

, 5 Dec 2006
Rate this:
Please Sign up or sign in to vote.
A TabControl in the Visual Studio 2005 style, which supports correct Right-To-Left (RTL) and Left-To-Right (LTR) drawing.

Sample Image - TabStrip.gif

Introduction

Besides all the new features of Visual Studio, there is a distinct TabControl with a new style like no other. The code here tries to build up a control which behaves like the tab pages of VS.NET 2005.

Building a TabControl consists of building two controls: a TabControl itself, which is a container control, but only accepts TabPages as a containing control. This could be done by assigning a Designer class to the TabControl. Now, TabPage is a container control too, but can accept all kinds of controls hosted in it.

The Visual Studio TabControl slightly differs from a normal tab control, because it only shows as much TabPages as fits the width of the control. All the other TabPages are available via a context menu appearing by clicking the glyph icon. Selecting a TabPage from the menu brings it to position 0 of the TabPageCollection, so that it appears as the first TabPage.

Right-To-Left Rendering of Controls

Adding the RTL drawing feature to a control requires additional coding for drawing. The developer should handle the correct drawing behavior when the control sets its RightToLeft property to Yes. The Graphics class has a method to draw strings. In RTL control drawing, it is important to use the below overload with the StringFormat setting its FormatFlags flag to DirectionRightToLeft. Note that since the StringFormat class is not a lightweight class, you should dispose it after using. It's more convenient to use a using block to achieve this:

e.Graphics.DrawString(string, Font, Brush, Rectangle, StringFormat);
using (HeavyWeightDisposableObject obj = 
       new HeavyWeightDisposableObject()) 
{
   //Do Operation and leave; will automatically dispose
}
//
// paint method of the control
//

using(StringFormat fmt = new StringFormat())
{
    fmt.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
    g.DrawString("Test String...", this.Font, 
                 SystemBrushes.Control, TextRectangle, fmt);
}

Design-Time Integration

For design-time behavior of the TabStrip control, I'm using a sub-class of the ParentControlDesigner class. It is nice to add the design-time verbs (e.g., Add/Remove TabPages) to this class, which makes life for the developer easier.

An additional step here is to remove the properties of the Control class which we do not want to be visible during design-time, by overriding the PreFilterProperties of the designer:

protected override void PreFilterProperties(
          System.Collections.IDictionary properties)
{
   base.PreFilterProperties(properties);

   properties.Remove("DockPadding");
   properties.Remove("DrawGrid");
   properties.Remove("Margin");
}

In a normal TabControl, the user can change the selected tab by clicking on the TabItems during design-time. To do this, we could override the GetHitTest(Point pt) method of the designer, but I don't know why it crashes my VS.NET IDE, so I had to think of another way. That's when tapping on Windows messages came in handy. Just override the WndProc(ref Message msg) method, and listen for the MouseLeftClick message (0x201), and check for a hit-test on TabItems:

protected override void WndProc(ref Message msg)
{
   if (msg.Msg == 0x201)
   {
      Point pt = Control.PointToClient(Cursor.Position);
      FATabStripItem itm = Control.GetTabItemByPoint(pt);
         
      if (itm != null)
      {
         Control.SelectedItem = itm;
         ArrayList selection = new ArrayList();
         selection.Add(itm);
         ISelectionService selectionService = 
           (ISelectionService)GetService(typeof(ISelectionService));
         selectionService.SetSelectedComponents(selection);
      }
   }

   base.WndProc(ref msg);
}

Points of Interest

If you want to use these controls on .NET 1.1, you'll have to change the use of ContextMenuStrip, which is a new control in .NET 2, with the old ContextMenu control. All the painting is done in GDI+, so it should work on Windows 98 or Windows 2000, although it is not tested yet.

For further releases or any suggestions/comments/feedbacks, please leave me a message at my address, or visit my web site.

History

Version 2

  • Bug fix: Problem with the Designer which made controls added to FATabStripItems disappear.
  • Bug fix: Disposing of heavy-weight objects when the control is being disposed (e.g., fonts, image, etc.) (reported by #sne#).
  • Bug fix: Problem with anchoring of controls/usercontrols in a FATabStripItems (reported by #sne#).
  • Added: FATabStripItems can now have an image which will be displayed on menu (thanks to danielku15).
  • Added: A HitTest method which will report which part of the control is clicked by the user.
  • Changes: Refactoring Selecting method of FATabItem.
Version 1
  • Providing base classes, and a TabStrip control with the Visual Studio 2005 theme implemented.
  • Providing a control-designer for TabStrip and TabStripItems.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Hadi Eskandari
Software Developer (Senior) Readify
Australia Australia
Working on both Java and .NET Technology, I have developed various enterprise level applications on both platforms. Currently, I am working as a Senior Software Developer at Readify which is a leading company on .NET technology in Australia.
Follow on   Twitter

Comments and Discussions

 
Questionmissing scrollbar when tab is window file explorer PinmemberJack Liu Angel12-Dec-14 18:05 
QuestionTab and contained controls not Disposed [modified] PinmemberSSDiver211224-Nov-14 8:05 
QuestionThank you PinmemberElizalde G. Baguinon16-Feb-14 15:27 
QuestionWhen More tabs and maximized window Pinmembervsridhara29-Jan-13 1:10 
QuestionMy Vote of 5 PinmemberMember 933872628-Jan-13 12:40 
QuestionAlignment PinmemberMember 88411264-Nov-12 2:08 
GeneralMy vote of 5 Pinmemberashe30716-Jul-12 6:10 
QuestionSelect a tab programmaticly Pinmemberhackerspk12-Jun-12 16:40 
QuestionJust add close button. PinmemberBehzad Sedighzadeh17-Dec-11 5:02 
Questionmono Pinmemberleosco20-Nov-11 23:20 
QuestionIt plans to add Tooltip to the TabPage (FATabStripItem)? Pinmembermiguelagl29-Sep-11 0:48 
QuestionProblem When Used With VS 2010 Pinmemberenkur18-Sep-11 13:59 
VS 2010 projects produce
 
Warning 3 The referenced assembly "E:\Projects\TabStrip\bin\Debug\TabStrip.dll" could not be resolved because it has a dependency on "System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project. Rolodex
 
when compiled.
 
VS 2005 projects work fine as do VS 2005 projects converted to VS 2010.
 
This is beyond my skills. Any advice?
 
Thanks
Enkur
AnswerRe: Problem When Used With VS 2010 PinmemberHadi Eskandari18-Sep-11 20:13 
GeneralRe: Problem When Used With VS 2010 Pinmemberenkur19-Sep-11 13:12 
GeneralRe: Problem When Used With VS 2010 PinmemberSanta's Little Helper20-Dec-11 5:37 
GeneralRe: Problem When Used With VS 2010 PinmemberMember 782672530-Jun-12 4:06 
QuestionAutoScroll, Draggable, Colored Tabs, Images On Bar PinmemberMichael Pelland14-Sep-11 3:38 
QuestionIncrease width and height of Tabstripitem's content area Pinmemberhuynhdangthai17-Jun-11 0:32 
GeneralMy vote of 5 PinmemberOmar Shraim19-Apr-11 11:21 
GeneralClose tab on middle button click Pinmembercd__ward11-Mar-11 2:27 
QuestionMouse enters tab strip [modified] Pinmembercd__ward11-Mar-11 2:08 
GeneralScrollable panel... Pinmembernathanieldamned3-Feb-11 6:54 
GeneralMy vote of 5 Pinmemberalifellod4-Nov-10 11:10 
GeneralProblem with Datagridview Pingroupi3dmax.com2-May-10 16:14 
GeneralJust what hat I had been looking for. PinmemberDiamonddrake13-Apr-10 19:06 
GeneralThanks! Pinmemberalatyami15-Feb-10 18:47 
QuestionHow can I get the close button per TabStripItem Pinmemberamit_vasu28-Oct-09 22:13 
AnswerRight-Border offset of selected tab (when over another) needs correction PinmemberMember 36108413-Aug-09 2:45 
QuestionHow can I move the tab bar to the bottom of the control? Pinmembercoby cai30-May-09 17:43 
QuestionVb.net translation PinmemberThe Lord of The Rings31-Jan-09 14:09 
GeneralProblem with FlowLayoutPanel and nested controls PinmemberKyronSr19-Jun-08 12:15 
GeneralRe: Problem with FlowLayoutPanel and nested controls PinmemberKyronSr23-Sep-08 14:49 
GeneralGood, ... Pinmemberzolfaghari28-Apr-08 3:03 
QuestionScrollable Panel? Pinmembermicheals20-Apr-08 0:20 
AnswerRe: Scrollable Panel? PinmemberJayke Huempfner4-Sep-11 18:11 
GeneralBug in FATabStripItemCollection Pinmemberdanielku1512-Mar-08 12:20 
Answergood PinmemberJeremyChin28-Jan-08 21:39 
GeneralNice code ! PinmemberBillWoodruff20-Jan-08 22:12 
QuestionHow to display images in the tabStrip? PinmemberPriyank Bolia16-Jan-08 8:04 
AnswerRe: How to display images in the tabStrip? Pinmemberdaniel030128-Nov-08 7:14 
GeneralRe: How to display images in the tabStrip? PinmemberPriyank Bolia10-Feb-09 13:05 
GeneralNew Tabs to the Right PinmemberSnerf5-Jan-08 16:14 
GeneralRe: New Tabs to the Right PinmemberKyronSr19-Jun-08 12:12 
GeneralGood control. Possible enhancments PinmemberGiorgi Dalakishvili27-Dec-07 7:06 
QuestionVery nice - License ?? PinmemberSunilKhatri4-Dec-07 2:57 
AnswerRe: Very nice - License ?? Pinmemberh.eskandari2-Jan-08 2:13 
GeneralIssue when try create a component which inherited from MSCTabStrip Pinmembernickxiaow20-Nov-07 10:27 
GeneralRe: Issue when try create a component which inherited from MSCTabStrip PinmemberHadi Eskandari20-Nov-07 20:36 
GeneralError: The designer loader did not provide a root component but has not indicated why. Pinmembernickxiaow20-Nov-07 10:12 
AnswerRe: Error: The designer loader did not provide a root component but has not indicated why. PinmemberHadi Eskandari20-Nov-07 20:29 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 5 Dec 2006
Article Copyright 2006 by Hadi Eskandari
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid