Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

vtTab: Tab Control Extender Class

, 25 May 2009 CPOL
A class used to extend the visual appearance of the tab control
vtTab.zip
vtTab.suo
vtTab
bin
Debug
vtTab.exe
vtTab.vshost.exe
vtTab.vshost.exe.manifest
Properties
Settings.settings
Resources
bg.bmp
pos1.bmp
tab1.png
tab2.bmp
tab3.png
tab4.png
vtTab.csproj.user
namespace vtTab
{
    #region Directives
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    #endregion

    public partial class frmMain : Form
    {
        #region Fields
        private Color _selectedColor = Color.CadetBlue;
        private Color _offsetColor = Color.White;
        private Color _foreColor = Color.Black;
        private cTabControl _tabExtender;
        #endregion

        #region Form Code
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_tabExtender != null)
                _tabExtender.Dispose();
        }


        private void frmMain_Load(object sender, EventArgs e)
        {
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            InitializeToolTips();
        }
        #endregion

        #region Style
        private void rbStyleBitmap_CheckedChanged(object sender, EventArgs e)
        {
            setTabStyle(0);
        }

        private void rbStyleVista_CheckedChanged(object sender, EventArgs e)
        {
            setTabStyle(1);
        }

        private void rbStyleCustom_CheckedChanged(object sender, EventArgs e)
        {
            setTabStyle(2);
        }

        private void rbStyleExplorer_CheckedChanged(object sender, EventArgs e)
        {
            setTabStyle(3);
        }

        private void setTabStyle(int index)
        {
            Blend bp = new Blend();
            switch (index)
            {
                // bitmap
                case 0:
                    // set to graphical
                    _tabExtender.TabDrawingStyle = TabRenderStyle.Graphic;
                    _tabExtender.TabFocusedForeColor = Color.Black;
                    _tabExtender.TabForeColor = Color.Black;
                    _tabExtender.TabSelectedForeColor = Color.Black;
                    _tabExtender.TabHeaderGraphic = Properties.Resources.tab3;
                    _tabExtender.TabBorderColor = Color.LightGray;
                    // tooltip styles
                    _tabExtender.ToolTipGradientBegin = Color.White;
                    _tabExtender.ToolTipGradientEnd = Color.LightSteelBlue;
                    _tabExtender.ToolTipForeColor = Color.Black;
                    break;
                // vista
                case 1:
                    // set to custom draw
                    _tabExtender.TabDrawingStyle = TabRenderStyle.Custom;
                    _tabExtender.TabBorderColor = Color.DarkGray;
                    _tabExtender.TabFocusedColor = Color.CornflowerBlue;
                    _tabExtender.TabFocusedForeColor = Color.White;
                    _tabExtender.TabSelectedColor = Color.White;
                    _tabExtender.TabForeColor = Color.Black;
                    _tabExtender.TabGradientBegin = Color.White;
                    _tabExtender.TabGradientEnd = Color.Silver;
                    bp.Positions = new float[] { 0f, .3f, .4f, .9f, 1f };
                    bp.Factors = new float[] { 0f, .2f, .5f, 1f, .6f };
                    _tabExtender.TabGradientBlend = bp;
                    // tooltip styles
                    _tabExtender.ToolTipGradientBegin = Color.White;
                    _tabExtender.ToolTipGradientEnd = Color.Silver;
                    _tabExtender.ToolTipForeColor = Color.Black;
                    break;
                // custom
                case 2:
                    _tabExtender.TabDrawingStyle = TabRenderStyle.Custom;
                    _tabExtender.TabBorderColor = Color.SteelBlue;
                    _tabExtender.TabFocusedColor = Color.White;
                    _tabExtender.TabFocusedForeColor = Color.Black;
                    _tabExtender.TabSelectedColor = Color.White;
                    _tabExtender.TabForeColor = Color.White;
                    _tabExtender.TabGradientBegin = Color.White;
                    _tabExtender.TabGradientEnd = Color.SteelBlue;
                    bp.Positions = new float[] { 0f, .1f, .2f, 1f };
                    bp.Factors = new float[] { 0f, .3f, .6f, .8f };
                    _tabExtender.TabGradientBlend = bp;
                    // tooltip styles
                    _tabExtender.ToolTipGradientBegin = Color.FromArgb(200, Color.White);
                    _tabExtender.ToolTipGradientEnd = Color.FromArgb(200, Color.SteelBlue);
                    _tabExtender.ToolTipForeColor = Color.FromArgb(240, 0x33, 0x33, 0x33);
                    break;
                // ie
                case 3:
                    // set to graphical
                    _tabExtender.TabDrawingStyle = TabRenderStyle.Graphic;
                    _tabExtender.TabFocusedForeColor = Color.Black;
                    _tabExtender.TabForeColor = Color.Black;
                    _tabExtender.TabSelectedForeColor = Color.Black;
                    _tabExtender.TabHeaderGraphic = Properties.Resources.tab4;
                    _tabExtender.TabBorderColor = Color.DarkGray;
                    // tooltip styles
                    _tabExtender.ToolTipGradientBegin = Color.White;
                    _tabExtender.ToolTipGradientEnd = Color.Silver;
                    _tabExtender.ToolTipForeColor = Color.Black;
                    break;
            }
            tabControl1.Refresh();
        }
        #endregion

        #region ToolTips
        private void chkTipIcon_CheckedChanged(object sender, EventArgs e)
        {
            if (_tabExtender != null)
                _tabExtender.ToolTipUseIcon = chkTipIcon.Checked;
        }

        private void chkTipTitle_CheckedChanged(object sender, EventArgs e)
        {
            InitializeToolTips();
        }

        private void InitializeToolTips()
        {
            // add tip text to pages
            for (int i = 0; i < tabControl1.TabCount; i++)
                tabControl1.TabPages[i].ToolTipText = "Test ToolTip " + (i + 1).ToString();
            // resets inbuilt tips
            _tabExtender.UseCustomToolTips(tabControl1);
            // tooltip styles
            _tabExtender.ToolTipUseIcon = true;
            _tabExtender.ToolTipGradientBegin = Color.White;
            _tabExtender.ToolTipGradientEnd = Color.Silver;
            _tabExtender.ToolTipForeColor = Color.Black;
            // add component controls
            if (chkTipTitle.Checked)
            {
                for (int i = 0; i < tabControl1.TabCount; i++)
                    _tabExtender.ToolTipTitle(tabControl1.TabPages[i], "This is example title " + (i + 1).ToString());
            }
            else
            {
                for (int i = 0; i < tabControl1.TabCount; i++)
                    _tabExtender.ToolTipTitle(tabControl1.TabPages[i], "");
            }
        }

        private void chkTipEnable_CheckedChanged(object sender, EventArgs e)
        {
            if (_tabExtender != null)
                _tabExtender.ToolTipEnable = chkTipEnable.Checked;
        }
        #endregion

        #region Control
        private void lblTabAdd_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.Add("Test Page " + (tabControl1.TabPages.Count + 1).ToString());
            tabControl1.TabPages[tabControl1.TabPages.Count - 1].BackColor = Color.White;
            if (tabControl1.TabPages.Count < 5)
                tabControl1.TabPages[tabControl1.TabPages.Count - 1].ImageIndex = tabControl1.TabPages.Count - 1;
        }

        private void lblTabAdd_MouseEnter(object sender, EventArgs e)
        {
            lblTabAdd.BackColor = _selectedColor;
            lblTabAdd.ForeColor = _offsetColor;
        }

        private void lblTabAdd_MouseLeave(object sender, EventArgs e)
        {
            lblTabAdd.BackColor = _offsetColor;
            lblTabAdd.ForeColor = _foreColor;
        }

        private void lblTabRemove_Click(object sender, EventArgs e)
        {
            if (tabControl1.TabPages.Count > 0)
                tabControl1.TabPages.RemoveAt(tabControl1.TabPages.Count - 1);
        }

        private void lblTabRemove_MouseEnter(object sender, EventArgs e)
        {
            lblTabRemove.BackColor = _selectedColor;
            lblTabRemove.ForeColor = _offsetColor;
        }

        private void lblTabRemove_MouseLeave(object sender, EventArgs e)
        {
            lblTabRemove.BackColor = _offsetColor;
            lblTabRemove.ForeColor = _foreColor;
        }

        private void lblTabReset_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();
            for (int i = 1; i < 10; i++)
            {
                tabControl1.TabPages.Add("Test Page " + i.ToString());
                if (i < 5)
                    tabControl1.TabPages[i - 1].ImageIndex = i - 1;
                tabControl1.TabPages[i - 1].BackColor = Color.White;
            }
        }

        private void lblTabReset_MouseEnter(object sender, EventArgs e)
        {
            lblTabReset.BackColor = _selectedColor;
            lblTabReset.ForeColor = _offsetColor;
        }

        private void lblTabReset_MouseLeave(object sender, EventArgs e)
        {
            lblTabReset.BackColor = _offsetColor;
            lblTabReset.ForeColor = _foreColor;
        }
        #endregion

        #region Properties
        private void chkPropEnabled_CheckedChanged(object sender, EventArgs e)
        {
            tabControl1.Enabled = chkPropEnabled.Checked;
            tabControl1.Refresh();
        }

        private void chkPropMultiline_CheckedChanged(object sender, EventArgs e)
        {
            tabControl1.Multiline = chkPropMultiline.Checked;
            // recreate
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            if (rbStyleBitmap.Checked)
                setTabStyle(0);
            else if (rbStyleVista.Checked)
                setTabStyle(1);
            else
                setTabStyle(2);
            InitializeToolTips();
            tabControl1.Refresh();
        }

        private void chkPropImages_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPropImages.Checked)
            {
                tabControl1.TabPages[0].ImageIndex = 1;
                tabControl1.TabPages[1].ImageIndex = 2;
                tabControl1.TabPages[2].ImageIndex = 3;
            }
            else
            {
                tabControl1.TabPages[0].ImageIndex = -1;
                tabControl1.TabPages[1].ImageIndex = -1;
                tabControl1.TabPages[2].ImageIndex = -1;
            }
        }

        #endregion

        #region Orientation
        private void lblOrientN_Click(object sender, EventArgs e)
        {
            posColorReset();
            lblOrientN.BackColor = _selectedColor;
            lblOrientN.ForeColor = _offsetColor;
            // change of orientation destroys the tab control..
            _tabExtender.Dispose();
            tabControl1.Alignment = TabAlignment.Top;
            // recreate
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            if (rbStyleBitmap.Checked)
                setTabStyle(0);
            else if (rbStyleVista.Checked)
                setTabStyle(1);
            else if (rbStyleCustom.Checked)
                setTabStyle(2);
            else
                setTabStyle(3);
            InitializeToolTips();
        }

        private void lblOrientS_Click(object sender, EventArgs e)
        {
            posColorReset();
            lblOrientS.BackColor = _selectedColor;
            lblOrientS.ForeColor = _offsetColor;
            _tabExtender.Dispose();
            tabControl1.Alignment = TabAlignment.Bottom;
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            if (rbStyleBitmap.Checked)
                setTabStyle(0);
            else if (rbStyleVista.Checked)
                setTabStyle(1);
            else if (rbStyleCustom.Checked)
                setTabStyle(2);
            else
                setTabStyle(3);
            InitializeToolTips();
        }

        private void lblOrientE_Click(object sender, EventArgs e)
        {
            posColorReset();
            lblOrientE.BackColor = _selectedColor;
            lblOrientE.ForeColor = _offsetColor;
            _tabExtender.Dispose();
            tabControl1.Alignment = TabAlignment.Left;
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            if (rbStyleBitmap.Checked)
                setTabStyle(0);
            else if (rbStyleVista.Checked)
                setTabStyle(1);
            else if (rbStyleCustom.Checked)
                setTabStyle(2);
            else
                setTabStyle(3);
            InitializeToolTips();
        }

        private void lblOrientW_Click(object sender, EventArgs e)
        {
            posColorReset();
            lblOrientW.BackColor = _selectedColor;
            lblOrientW.ForeColor = _offsetColor;
            _tabExtender.Dispose();
            tabControl1.Alignment = TabAlignment.Right;
            _tabExtender = new cTabControl(tabControl1.Handle, Properties.Resources.tab1);
            if (rbStyleBitmap.Checked)
                setTabStyle(0);
            else if (rbStyleVista.Checked)
                setTabStyle(1);
            else if (rbStyleCustom.Checked)
                setTabStyle(2);
            else
                setTabStyle(3);
            InitializeToolTips();
        }

        private void posColorReset()
        {
            lblOrientN.BackColor = _offsetColor;
            lblOrientN.ForeColor = _foreColor;
            lblOrientS.BackColor = _offsetColor;
            lblOrientS.ForeColor = _foreColor;
            lblOrientE.BackColor = _offsetColor;
            lblOrientE.ForeColor = _foreColor;
            lblOrientW.BackColor = _offsetColor;
            lblOrientW.ForeColor = _foreColor;
        }
        #endregion
    }
}

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)

Share

About the Author

John Underhill
Network Administrator vtdev.com
Canada Canada
Network and programming specialist. Started in C, and have learned about 14 languages since then. Cisco programmer, and lately writing a lot of C# and WPF code, (learning Java too). If I can dream it up, I can probably put it to code. My software company, (VTDev), is on the verge of releasing a couple of very cool things.. keep you posted.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 25 May 2009
Article Copyright 2009 by John Underhill
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid