Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Customised TabControl using C#

Rate me:
Please Sign up or sign in to vote.
2.40/5 (6 votes)
9 Jul 2008CPOL 82.8K   2.4K   15  
Customised TabControl using C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ColorTabControlExample
{
    public partial class CTabControl : System.Windows.Forms.TabControl
    {
        public CTabControl()
        {
            this.ItemSize = new Size(80, 30);
            this.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.DrawItem += new DrawItemEventHandler(this.RepaintControls);

            this.Invalidate();
        }


        private void RepaintControls(object sender, DrawItemEventArgs e)
        {
            try
            {
                Font font;
                Brush back_brush;
                Brush fore_brush;
                Rectangle bounds = e.Bounds;

                this.TabPages[e.Index].BackColor = Color.Silver;

                if (e.Index == this.SelectedIndex)
                {
                    font = new Font(e.Font, e.Font.Style);
                    back_brush = new SolidBrush(Color.DimGray);
                    fore_brush = new SolidBrush(Color.White);
                    bounds = new Rectangle(bounds.X + (this.Padding.X / 2), bounds.Y + this.Padding.Y, bounds.Width - this.Padding.X, bounds.Height - (this.Padding.Y * 2));
                }
                else
                {
                    font = new Font(e.Font, e.Font.Style & ~FontStyle.Bold);
                    back_brush = new SolidBrush(this.TabPages[e.Index].BackColor);
                    fore_brush = new SolidBrush(this.TabPages[e.Index].ForeColor);
                }

                string tab_name = this.TabPages[e.Index].Text;
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                e.Graphics.FillRectangle(back_brush, bounds);
                e.Graphics.DrawString(tab_name, font, fore_brush, bounds, sf);
               
                Brush background_brush = new SolidBrush(Color.DodgerBlue);
                Rectangle LastTabRect = this.GetTabRect(this.TabPages.Count - 1);
                Rectangle rect = new Rectangle();
                rect.Location = new Point(LastTabRect.Right + this.Left, this.Top );
                rect.Size = new Size(this.Right - rect.Left, LastTabRect.Height);
                e.Graphics.FillRectangle(background_brush, rect);
                background_brush.Dispose();
                
                sf.Dispose();
                back_brush.Dispose();
                fore_brush.Dispose();
                font.Dispose();
            }
            catch
            { }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer Wittmann-Robot
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions