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

Coloring Tab Control

Rate me:
Please Sign up or sign in to vote.
4.13/5 (63 votes)
14 Sep 2004CPOL2 min read 278.4K   6.3K   50   36
Coloring Tab Control

Introduction

Tabcontrol is not at all unknown to windows developers. Microsoft included this control from Visual Studio to Visual Studio.NET SDK to enhance developers productivity and end users better experience. I agree that our windows development must flow some standard to give better user experience. Here I am trying to give little more enhancement on control appearance if and only if developer want to do it.

Background

This article is small in size and can apply in your different application only if it must be a winform development. You can refer this article to update your tab control's appearances in your parent window.

By default or through any properties the existing VS.NET SDK won't support features which will enhance the appearances of tab pages like changing font appearances , color,caption back color etc. Here what I am trying to do is , override a little and got into solution. So you can use this idea to enhance your product or project.

If you feel this is good then don't forget to vote and give put your valuable suggestions.

If you check the property list of tabcontrol. You will came to to about DrawMode property. DrawMode property decides that how tabpages are drawn in your application. If you check the values then you will came to that there is two values are there which is assignable for the property DrawMode.

DrawMode Enumeration

Normal

This enumerated value will specify that operating system will decide to draw on behalf of developers.

OwnerDrawn

This enumerated value will specify that developer is responsible for drawing the appearance of tab page.

Suppose if you want to draw by yourself then you need to define event handler for TabControl's DrawItem Event.

Here also I am doing the same thing. Suppose it you want to change the appearance of tabpage of any tabcontrol then you need to set

  1. DrawMode property to OwnerDrawFixed.
  2. Override the DrawItem event handler definition.

Source Code

C#
private void tabControl1_DrawItem(object sender, 
  System.Windows.Forms.DrawItemEventArgs e)
   {
       try
        {
          //This line of code will help you to 
          //change the apperance like size,name,style.
          Font f;
          //For background color
          Brush backBrush;
          //For forground color
          Brush foreBrush;
          //This construct will hell you to decide 
          //which tab page have current focus
          //to change the style.
          if(e.Index == this.tabControl1.SelectedIndex)
          {
          //This line of code will help you to 
          //change the apperance like size,name,style.
          f = new Font(e.Font, FontStyle.Bold | FontStyle.Bold);
          f = new Font(e.Font,FontStyle.Bold);
          backBrush = new System.Drawing.SolidBrush(Color.DarkGray);
          foreBrush = Brushes.White;
        }
        else
        {
          f = e.Font;
          backBrush = new SolidBrush(e.BackColor); 
          foreBrush = new SolidBrush(e.ForeColor);
        }
          //To set the alignment of the caption.
          string tabName = this.tabControl1.TabPages[e.Index].Text;
          StringFormat sf = new StringFormat();
          sf.Alignment = StringAlignment.Center;
          //Thsi will help you to fill the interior portion of
          //selected tabpage.
//Continue.........
  }
}

Note

Full source code with an example is given with this article.

License

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


Written By
Software Developer (Senior) Freelance
India India
He is a certified professional in both MCPD and MCTS. He is a mathematics graduate with masters in computer science.He was born and bred in India and happen to spend some time in Europe.

Comments and Discussions

 
GeneralMy vote of 5 Pin
MUMU8889-Apr-13 13:41
MUMU8889-Apr-13 13:41 
GeneralMy vote of 5 Pin
VivianHuang29-Aug-12 21:56
VivianHuang29-Aug-12 21:56 
GeneralMy vote of 5 Pin
Member 79493628-Jun-11 1:05
Member 79493628-Jun-11 1:05 
GeneralThanks for the article Pin
DeadlyB12-May-09 5:26
DeadlyB12-May-09 5:26 
GeneralProblem When RightToLeftLayout [modified] Pin
Aeyd M.24-Sep-07 23:14
Aeyd M.24-Sep-07 23:14 
GeneralRe: Problem When RightToLeftLayout Pin
sreejith ss nair25-Sep-07 17:53
sreejith ss nair25-Sep-07 17:53 
GeneralRe: Problem When RightToLeftLayout Pin
sreejith ss nair7-Oct-07 18:19
sreejith ss nair7-Oct-07 18:19 
GeneralArticle Extension Pin
ag1h3-Feb-07 5:44
ag1h3-Feb-07 5:44 
GeneralRe: Article Extension Pin
sreejith ss nair25-Sep-07 17:50
sreejith ss nair25-Sep-07 17:50 
GeneralRe: Article Extension Pin
sreejith ss nair7-Oct-07 18:19
sreejith ss nair7-Oct-07 18:19 
GeneralBackcolor property Pin
MBrooker26-Oct-06 8:20
MBrooker26-Oct-06 8:20 
GeneralRe: Backcolor property Pin
sreejith ss nair25-Sep-07 17:51
sreejith ss nair25-Sep-07 17:51 
GeneralRe: Backcolor property Pin
sreejith ss nair7-Oct-07 18:20
sreejith ss nair7-Oct-07 18:20 
Generalredrawing giving awfull look to application Pin
khaadem24-Jan-06 3:11
khaadem24-Jan-06 3:11 
GeneralRe: redrawing giving awfull look to application Pin
sreejith ss nair24-Jan-06 3:24
sreejith ss nair24-Jan-06 3:24 
GeneralRe: redrawing giving awfull look to application Pin
khaadem24-Jan-06 3:49
khaadem24-Jan-06 3:49 
GeneralRe: redrawing giving awfull look to application Pin
sreejith ss nair30-Jan-06 22:10
sreejith ss nair30-Jan-06 22:10 
GeneralRe: redrawing giving awfull look to application Pin
sreejith ss nair7-Oct-07 18:20
sreejith ss nair7-Oct-07 18:20 
QuestionHow about the rest of the tab control Pin
John011-Dec-04 7:07
John011-Dec-04 7:07 
AnswerRe: How about the rest of the tab control Pin
hcihlen5-Sep-05 11:53
hcihlen5-Sep-05 11:53 
GeneralRe: How about the rest of the tab control Pin
rohinivn19-Mar-07 19:33
rohinivn19-Mar-07 19:33 
GeneralRe: How about the rest of the tab control Pin
rohinivn19-Mar-07 22:20
rohinivn19-Mar-07 22:20 
GeneralRe: How about the rest of the tab control Pin
sreejith ss nair7-Oct-07 18:23
sreejith ss nair7-Oct-07 18:23 
AnswerRe: How about the rest of the tab control Pin
sreejith ss nair7-Oct-07 18:20
sreejith ss nair7-Oct-07 18:20 
GeneralDerived TabPage Pin
Guggsdu28-Oct-04 2:51
Guggsdu28-Oct-04 2:51 

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.