Click here to Skip to main content
15,885,435 members
Articles / Programming Languages / C#
Article

A TabControl with tab page closing capability

Rate me:
Please Sign up or sign in to vote.
3.68/5 (11 votes)
12 Dec 20051 min read 202.5K   4.3K   48   18
An extension of the tab control that adds a closing 'X' at the left side of the tab page: pressing it will close the tab page.

Sample Image

Introduction

I was writing an application that uses a TabControl. I needed to give my user the ability to close tab pages dynamically, so I was looking for a tab control that has a close sign on every page, but could not find one. So I started developing one. The result of this development is the following class: TabControlEx.

Using the code

To use the the tab control, I perform the following action:

  1. Add a regular TabControl to my form.
  2. Edit the Designer file (if your form name is Form1, you need to edit the Form1.Designer.cs file).

You need to change the type of the tab control from "System.Windows.Forms.TabControl" to "Yoramo.GuiLib.TabControlEx". You also need to change the instantiation statement (in the "InitializeComponent" method) to create the correct type of tab control.

The TabControlEx Code

TabControlEx derives from System.Windows.Forms.TabControl. It implements the OnDrawItem to draw the 'x' sign on the page (Close sign) and the OnMouseClick to figure out if the close sign has been clicked. In the constructor, I have changed the DrawMode to TabDrawMode.OwnerDrawFixed to activate the OnDrawItem. In order to enable the application to refuse closing a tab, or to provide an event to enable saving data, I have defined an event called PreRemoveTabPage.

C#
using System;
using System.Windows.Forms;
using System.Drawing;

namespace Yoramo.GuiLib
{
    public delegate bool PreRemoveTab(int indx);
    public class TabControlEx : TabControl
    {
        public TabControlEx()
            : base()
        {
            PreRemoveTabPage = null;
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
        }

        public PreRemoveTab PreRemoveTabPage;

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Rectangle r = e.Bounds;
            r = GetTabRect(e.Index);
            r.Offset(2, 2);
            r.Width = 5;
            r.Height = 5;
            Brush b = new SolidBrush(Color.Black);
            Pen p = new Pen(b);
            e.Graphics.DrawLine(p, r.X, r.Y, r.X + r.Width, r.Y + r.Height);
            e.Graphics.DrawLine(p, r.X + r.Width, r.Y, r.X, r.Y + r.Height);

            string titel = this.TabPages[e.Index].Text;
            Font f = this.Font;
            e.Graphics.DrawString(titel, f, b, new PointF(r.X + 5, r.Y));
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            Point p = e.Location;
            for (int i = 0; i < TabCount; i++)
            {
                Rectangle r = GetTabRect(i);
                r.Offset(2, 2);
                r.Width = 5;
                r.Height = 5;
                if (r.Contains(p))
                {
                    CloseTab(i);
                }
            }
        }

        private void CloseTab(int i)
        {
            if (PreRemoveTabPage != null)
            {
                bool closeIt = PreRemoveTabPage(i);
                if (!closeIt)
                    return;
            }
            TabPages.Remove(TabPages[i]);
        }
    }
}

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


Written By
Architect
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks Pin
wilmarux20-Jun-13 10:02
wilmarux20-Jun-13 10:02 
Newssources of a similar control Pin
jrocnuck28-Mar-07 16:42
jrocnuck28-Mar-07 16:42 
Questiongreat code, image? Pin
Norberto Olazabal28-Mar-07 16:29
Norberto Olazabal28-Mar-07 16:29 
AnswerRe: great code, image? Pin
Yoramo31-Mar-07 4:22
Yoramo31-Mar-07 4:22 
GeneralTight code Pin
skeletal4527-Feb-07 23:30
skeletal4527-Feb-07 23:30 
GeneralOnMouseDown -- no valid operation Pin
Fish19813-May-06 11:36
Fish19813-May-06 11:36 
GeneralRe: OnMouseDown -- no valid operation Pin
Fish19813-May-06 11:43
Fish19813-May-06 11:43 
AnswerRe: OnMouseDown -- no valid operation Pin
Yoramo3-May-06 13:55
Yoramo3-May-06 13:55 
GeneralRe: OnMouseDown -- no valid operation Pin
Fish19815-May-06 4:03
Fish19815-May-06 4:03 
GeneralGreat Code Pin
firedraken23-Mar-06 18:08
firedraken23-Mar-06 18:08 
GeneralRe: Great Code Pin
Yoramo23-Mar-06 19:00
Yoramo23-Mar-06 19:00 
GeneralRe: Great Code Pin
firedraken23-Mar-06 19:45
firedraken23-Mar-06 19:45 
thats what i thought. But when i replaced private System.Windows.Forms.TabControl; with private Yaramo.GuiLib.TabControlEx; it gave me errors and would not display my form.

-- modified at 1:48 Friday 24th March, 2006

Also, where would i modify the InitializeComponent at? yes I am relativley new to c#

-- modified at 1:52 Friday 24th March, 2006

Here is the error it gives me
Error 1 Invalid token ';' in class, struct, or interface member declaration F:\My Programs\mybrowser\mybrowser\Form1.Designer.cs 462 43 mybrowser


this is the only error that comes up
GeneralRe: Great Code Pin
Yoramo24-Mar-06 6:36
Yoramo24-Mar-06 6:36 
GeneralVisual style lost Pin
Nikhil Contractor13-Mar-06 22:53
professionalNikhil Contractor13-Mar-06 22:53 
GeneralRe: Visual style lost Pin
Yoramo23-Mar-06 18:56
Yoramo23-Mar-06 18:56 
GeneralRe: Visual style lost Pin
zvi12346720-Sep-10 23:27
zvi12346720-Sep-10 23:27 
Generalw00t Pin
Berger20064-Jan-06 9:44
Berger20064-Jan-06 9:44 
GeneralRe: w00t Pin
Yoramo14-Jan-06 21:22
Yoramo14-Jan-06 21:22 

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.