Click here to Skip to main content
6,305,776 members and growing! (15,730 online)
Email Password   helpLost your password?
Desktop Development » Tabs & Property Pages » Tabs and Property Pages     Intermediate

A TabControl with tab page closing capability

By Yoramo

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.
C#.NET 2.0, WinXPVS2005, Dev
Posted:12 Dec 2005
Views:64,934
Bookmarked:30 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
7 votes for this article.
Popularity: 3.16 Rating: 3.74 out of 5
1 vote, 14.3%
1
2 votes, 28.6%
2

3
1 vote, 14.3%
4
3 votes, 42.9%
5

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


Member

Occupation: Architect
Location: Israel Israel

Other popular Tabs & Property Pages articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 16 of 16 (Total in Forum: 16) (Refresh)FirstPrevNext
Newssources of a similar control Pinmemberjrocnuck17:42 28 Mar '07  
Questiongreat code, image? PinmemberNorberto Olazabal17:29 28 Mar '07  
AnswerRe: great code, image? PinmemberYoramo5:22 31 Mar '07  
GeneralTight code Pinmemberskeletal450:30 28 Feb '07  
GeneralOnMouseDown -- no valid operation PinmemberFish198112:36 3 May '06  
GeneralRe: OnMouseDown -- no valid operation PinmemberFish198112:43 3 May '06  
AnswerRe: OnMouseDown -- no valid operation PinmemberYoramo14:55 3 May '06  
GeneralRe: OnMouseDown -- no valid operation PinmemberFish19815:03 5 May '06  
GeneralGreat Code Pinmemberfiredraken19:08 23 Mar '06  
GeneralRe: Great Code PinmemberYoramo20:00 23 Mar '06  
GeneralRe: Great Code Pinmemberfiredraken20:45 23 Mar '06  
GeneralRe: Great Code PinmemberYoramo7:36 24 Mar '06  
GeneralVisual style lost PinmemberNikhil_be_IT23:53 13 Mar '06  
GeneralRe: Visual style lost PinmemberYoramo19:56 23 Mar '06  
Generalw00t PinmemberBerger200610:44 4 Jan '06  
GeneralRe: w00t PinmemberYoramo22:22 14 Jan '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Dec 2005
Editor: Smitha Vijayan
Copyright 2005 by Yoramo
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project