Click here to Skip to main content
13,357,380 members (81,339 online)
Click here to Skip to main content
Add your own
alternative version


181 bookmarked
Posted 16 Aug 2007

FireFox-like Tab Control

, 24 Jun 2008
Rate this:
Please Sign up or sign in to vote.
An article on Tab Control

Screenshot - image001.png


Recently I came across a requirement for a tab control to be closed from the header itself. This means the user doesn't need to switch to any tab page in order to close it. The perfect example for this one is Firefox browser. In Firefox, the user can open as many tabs as he wants and he can close any tab at any time without opening the tab. I tried Googling a solution and didn't find anything that was exactly what I wanted. Then I thought about implementing my own tab control with the same functionality. So finally, here is the control.

Using the Code

The control enables the designer to add/remove tab pages like a normal tab control with the new look and feel. The classes that support this features are:

  1. TabCtlEx.cs (inherited from System.Windows.Forms.TabControl)
  2. TabPage.cs (inherited from System.Windows.Forms.TabPage)

Using this control is straightforward, like the .NET tab control.

private MyControlLibrary.TabCtlEx userControl11;
    TabPageEx tabPage1;
    TabPageEx tabPage2;
    TabPageEx tabPage3;

private TabPageEx tabPage4;
    this.userControl11 = new MyControlLibrary.TabCtlEx();
    this.tabPage1 = new MyControlLibrary.TabPageEx(this.components);
    this.tabPage2 = new MyControlLibrary.TabPageEx(this.components);
    this.tabPage3 = new MyControlLibrary.TabPageEx(this.components);
    this.tabPage4 = new MyControlLibrary.TabPageEx(this.components);
    this.Controls.Add(this. userControl11);

Drawing the Close Button on Each Tab Header

This requires you to override the existing OnDrawItem() function in the .NET tab control.

/// <span class="code-SummaryComment"><summary></span>
/// override to draw the close button
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="e"></param></span>
protected override void OnDrawItem(DrawItemEventArgs e)
    RectangleF tabTextArea = RectangleF.Empty;
    for(int nIndex = 0 ; nIndex < this.TabCount ; nIndex++)
        if( nIndex != this.SelectedIndex )
            /*if not active draw ,inactive close button*/
            tabTextArea = (RectangleF)this.GetTabRect(nIndex);
            using(Bitmap bmp = new Bitmap(GetContentFromResource(
                    tabTextArea.X+tabTextArea.Width -16, 5, 13, 13);
            tabTextArea = (RectangleF)this.GetTabRect(nIndex);
            LinearGradientBrush br = new LinearGradientBrush(tabTextArea,

            /*if active draw ,inactive close button*/
            using(Bitmap bmp = new Bitmap(
                    tabTextArea.X+tabTextArea.Width -16, 5, 13, 13);
        string str = this.TabPages[nIndex].Text;
        StringFormat stringFormat = new StringFormat();f
        stringFormat.Alignment = StringAlignment.Center; 
        using(SolidBrush brush = new SolidBrush(
            /*Draw the tab header text
            e.Graphics.DrawString(str,this.Font, brush,

Here, the close button is actually the bitmap image drawn over each tab header. So, in just giving the look and feel of a button used in Firefox, three different bitmap images are used:

  • closeinactive.bmp
  • close.bmp
  • onhover.bmp

These images are embedded with the control and extracted using reflection. There is also a function used to get the embedded image resource. This function returns the stream and creates an image by passing the stream to a bitmap class, as described above.

/// <span class="code-SummaryComment"><summary></span>
/// Get the stream of the embedded bitmap image
/// <span class="code-SummaryComment"></summary></span>
/// <span class="code-SummaryComment"><param name="filename"></param></span>
/// <span class="code-SummaryComment"><returns></returns></span>
private Stream GetContentFromResource(string filename)
    Assembly asm = Assembly.GetExecutingAssembly();
    Stream stream =asm.GetManifestResourceStream(
    return stream;

Firefox asks the user to confirm whether he wants to close the tab page or not. I like to have the same functionality for my control, since sometimes the user may press the close button accidentally and doesn't want to lose the changes done. This is accomplished by setting the Boolean property.

private bool confirmOnClose = true;
public bool ConfirmOnClose
        return this.confirmOnClose;
        this.confirmOnClose = value;


ConfirmOnClose confirms with a message box before closing the tab page. Here is the code to check if the clicked area is inside the bitmap rectangle or not.

protected override void OnMouseDown(MouseEventArgs e)
    RectangleF tabTextArea = (RectangleF)this.GetTabRect(SelectedIndex);
    tabTextArea = 
        new RectangleF(tabTextArea.X+tabTextArea.Width -16,5,13,13);
    Point pt = new Point(e.X,e.Y);
            if(MessageBox.Show("You are about to close "+
                " tab. Are you sure you want to continue?","Confirm close",
                MessageBoxButtons.YesNo) == DialogResult.No)
        //Fire Event to Client
        if(OnClose != null)
            OnClose(this,new CloseEventArgs(SelectedIndex));

Whenever the close button on the tab header is pressed, the control will fire an OnClose() event to the client with the clicked tab index as the argument. This will give some more flexibility to the client to do something before closing the tab.


It's just yet another tab control that provides some flexibility and an improved user interface. I found this functionality very useful in some scenarios. Feedbacks are welcome!


Updated with menu button functionality. The above description is just for adding the close button, but the article source code contains the actual implementation for both the close button and the menu button. The major advantage with this control is that menu item functionality is applied to each tab page instead of tab control. In other words, we can have different menu items for different tab pages.

If there is no menu attached with any of the tab pages, it won't display the menu button. The below picture shows that tabPage3 doesn't have a menu attached and so doesn't display the Menu button for that tab page only.

Screenshot - image004.jpg

The actual code for attaching the menu item is:

    = contextMenuStrip1;
    = contextMenuStrip1;
    = contextMenuStrip1;
    = contextMenuStrip1;

Either you can assign the same menu item or a different menu item for each tab page.


  • 16 August, 2007 -- Original version posted
  • 28 August, 2007 -- Article and download updated
  • 24 June, 2008 -- source updated


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


About the Author

Software Developer (Senior)
United States United States
worst fellow working as a consultant in a Telecom company (USA)
knows little bit on C,C++ and C# with 5+ years of experience.

You may also be interested in...


Comments and Discussions

Generalset tab size Pin
Member 23518431-Jul-08 0:42
memberMember 23518431-Jul-08 0:42 
GeneralRe: set tab size Pin
Ilíon1-Jul-08 2:10
memberIlíon1-Jul-08 2:10 
GeneralRe: set tab size Pin
Member 23518431-Jul-08 20:22
memberMember 23518431-Jul-08 20:22 
GeneralI'm sorry [modified] Pin
Ilíon2-Jul-08 8:18
memberIlíon2-Jul-08 8:18 
GeneralRe: set tab size Pin
Member 23518431-Jul-08 21:32
memberMember 23518431-Jul-08 21:32 
GeneralRe: set tab size Pin
Ilíon29-Jul-08 9:59
memberIlíon29-Jul-08 9:59 
GeneralRe: set tab size Pin
Member 235184329-Jul-08 20:37
memberMember 235184329-Jul-08 20:37 
GeneralHCI issue Pin
Derek Bartram25-Jun-08 2:17
memberDerek Bartram25-Jun-08 2:17 
Is making a new tab really necassery on every tab header. I thought generally you only have one button for each piece of functionality; not that this doesn't apply to things like close other tabs since even though the functionality is similar it has a significant difference (due to context).

Ps. My firefox tabs have never looked like that; the scroll have been at either end (i.e. left scroll at left and right scroll at right).

Pps. The design looks a little clunky as well; you could perhaps try styling it a little more cleanly.

Good article Smile | :)
GeneralRe: HCI issue Pin
vijayaprasen28-Jun-08 5:57
membervijayaprasen28-Jun-08 5:57 
GeneralException Pin
gborges20-May-08 11:11
membergborges20-May-08 11:11 
GeneralRe: Exception Pin
vijayaprasen20-May-08 14:34
membervijayaprasen20-May-08 14:34 
GeneralRe: Exception; suggested solution [modified] Pin
Ilíon27-Jun-08 5:26
memberIlíon27-Jun-08 5:26 
GeneralRe: Exception; suggested solution Pin
sth_Weird18-Feb-09 0:18
membersth_Weird18-Feb-09 0:18 
GeneralRe: Exception; suggested solution Pin
Ilíon18-Feb-09 1:14
memberIlíon18-Feb-09 1:14 
QuestionOwner project Pin
eusta12-Apr-08 4:06
membereusta12-Apr-08 4:06 
GeneralRe: Owner project Pin
vijayaprasen12-Apr-08 15:17
membervijayaprasen12-Apr-08 15:17 
GeneralException Pin
Gnanadurai12-Dec-07 3:09
memberGnanadurai12-Dec-07 3:09 
GeneralRe: Exception Pin
vijayaprasen25-Dec-07 12:16
membervijayaprasen25-Dec-07 12:16 
GeneralRe: Exception Pin
Gnanadurai25-Dec-07 18:03
memberGnanadurai25-Dec-07 18:03 
GeneralRe: Exception Pin
vijayaprasen26-Dec-07 10:47
membervijayaprasen26-Dec-07 10:47 
GeneralSmall correction needed... Pin
Leon v Wyk29-Nov-07 3:46
memberLeon v Wyk29-Nov-07 3:46 
GeneralRe: Small correction needed... Pin
vijayaprasen30-Nov-07 12:32
membervijayaprasen30-Nov-07 12:32 
GeneralPerfect Job Pin
Behzad Sedighzadeh8-Nov-07 2:36
memberBehzad Sedighzadeh8-Nov-07 2:36 
GeneralRe: Perfect Job Pin
vijayaprasen9-Nov-07 4:31
membervijayaprasen9-Nov-07 4:31 
GeneralVS2005 compatibility Pin
Droopy21-Sep-07 3:47
memberDroopy21-Sep-07 3:47 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 24 Jun 2008
Article Copyright 2007 by vijayaprasen
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid