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

A TabControl with tab page closing capability

By , 12 Dec 2005
 

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.

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

About the Author

Yoramo
Architect
Israel Israel
Member
No Biography provided

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

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Newssources of a similar controlmemberjrocnuck28 Mar '07 - 16:42 
Hi... I realize this is C#, but did you ever think of looking at the source code to the EMule project? They have a closable tab control. Porting to C# shouldn't be that difficult.

 
Eric Nuckols

Questiongreat code, image?memberNorberto Olazabal28 Mar '07 - 16:29 
i added an image as the close x but i cant change the tab to width to fit the image and text
 
do you know how can i make the Width change ?
 
thank you
AnswerRe: great code, image?memberYoramo31 Mar '07 - 4:22 
I did not found a way to control the width of the page directly, but there is a workaround:
1 drive the TabPage and have a new string member 'DisplayText'
2 the tab control will measure the 'Text' property and you can then display the new property instead. make sute the lenght of the 'Text'.Length >= 'DisplayText'.Length
GeneralTight codememberskeletal4527 Feb '07 - 23:30 
Thank you very much Yorumo, great stuff!!!!!
 
c",) skeletal

GeneralOnMouseDown -- no valid operationmemberFish19813 May '06 - 11:36 
TabControlEx.OnMouseClick(System.Windows.Forms.MouseEventArgs)': no suitable method found to override.
 

Not sure why this doesn't pick up the TabControl's OnMouseClick operation.
GeneralRe: OnMouseDown -- no valid operationmemberFish19813 May '06 - 11:43 
I'm using VS.Net 2003, is this only valid in 2005?
AnswerRe: OnMouseDown -- no valid operationmemberYoramo3 May '06 - 13:55 
Here is a version the will work on VS2003 it is less elegant but that’s the solution I found.
 
===========================================================
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;
_lastMouse = Point.Empty;
}
 
public PreRemoveTab PreRemoveTabPage;
private Point _lastMouse;
 
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));
}
 
// use this method instead of the 'OnMouseClick' which is not supported on VS2003!!!
protected override void OnClick(EventArgs e)
{
Point p = _lastMouse;
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);
}
}
}
 

// record the mouse location
protected override void OnMouseMove(MouseEventArgs e)
{
_lastMouse = new Point(e.X,e.Y);
base.OnMouseMove (e);
}
 
/* can not overide it on VS2003
protected override void OnMouseClick(MouseEventArgs e)
{
...
}
*/
private void CloseTab(int i)
{
if (PreRemoveTabPage != null)
{
bool closeIt = PreRemoveTabPage(i);
if (!closeIt)
return;
}
TabPages.Remove(TabPages[i]);
}
}
}

===========================================================
GeneralRe: OnMouseDown -- no valid operationmemberFish19815 May '06 - 4:03 
Thank you, this works well.
GeneralGreat Codememberfiredraken23 Mar '06 - 18:08 
This is a great code and it will do exactly what I need it to but where do I extract it to and do I import it into my project or do a function call? Help please.
GeneralRe: Great CodememberYoramo23 Mar '06 - 19:00 
you can just take the TabControlEx.cs and place it in your project and start using it.
GeneralRe: Great Codememberfiredraken23 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 CodememberYoramo24 Mar '06 - 6:36 
it looks like a simple problem. send me a minimal zip with the problem and I will take a look.
GeneralVisual style lostmemberNikhil_be_IT13 Mar '06 - 22:53 
Hi,
When you ser DrwMode as OwnerDrawFixed it lost the winXP visual style. can you please guide how to reactivate it.
GeneralRe: Visual style lostmemberYoramo23 Mar '06 - 18:56 
It looks that when using a ownerdraw mode you lose the styles drawing.
I have tried to keep the style but with no success.
if i will find a way i will return and update you.
 
Yoramo
GeneralRe: Visual style lostmemberzvi12346720 Sep '10 - 23:27 
In case you don't want to lost the visual just write at the tab name - "TabName X"
and insert the following code to the MouseClick event -

private void TheTabControl_MouseClick(object sender, MouseEventArgs e)
{
Point p = e.Location;
Rectangle r = Windows.GetTabRect(TheTabControl.SelectedIndex);
r.Offset(r.Width - 16, 1);
r.Width = 16;
r.Height = 16;
if (r.Contains(p))
{
TheTabControl.TabPages.RemoveAt(TheTabControl.SelectedIndex);
}
}
That will check if the point is on the X
Its depense the font you are using, you just need to update the "16" to the size of yours font
Generalw00tmemberBerger20064 Jan '06 - 9:44 
Nice, Exactly what I was looking for Smile | :)
GeneralRe: w00tmemberYoramo14 Jan '06 - 21:22 
Thanks for your comment

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 12 Dec 2005
Article Copyright 2005 by Yoramo
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid