My Photo
Location: Bangalore, India

Tips to Reduce Abdominal Fat!

Monday, July 03, 2006

How to change the color of Tab Control (in c#)

The Tab Control in .net does not expose properties to change its back ground and foreground color. This can be achieved by overriding the DrawItem event of the control. Following are the steps involved to do the same.
1. Set the TabControl's DrawMode to OwnerDraw
2. Handle the DrawItem event to draw things yourself
You may call the following method at the DrawItem event of tab control to test this.

private void ChangeTabColor(DrawItemEventArgs e)
Font TabFont;
Brush BackBrush = new SolidBrush(Color.Green); //Set background color
Brush ForeBrush = new SolidBrush(Color.Yellow);//Set foreground color
if (e.Index == this.tabControl1.SelectedIndex)
TabFont = new Font(e.Font, FontStyle.Italic FontStyle.Bold);
TabFont = e.Font;
string TabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.FillRectangle(BackBrush, e.Bounds);
Rectangle r = e.Bounds;
r = new Rectangle(r.X, r.Y + 3, r.Width, r.Height - 3);
e.Graphics.DrawString(TabName, TabFont, ForeBrush, r, sf);
//Dispose objects
if (e.Index == this.tabControl1.SelectedIndex)


Blogger John Angelini said...

Good Post. There is one error however that prevents it from building.

The line:
TabFont = new Font(e.Font, FontStyle.Italic FontStyle.Bold);

Should be (with the flag switch "|"):
TabFont = new Font(e.Font, FontStyle.Italic | FontStyle.Bold);

Additionally, if you changed the signature of the method to:
private void ChangeTabColor(object sender, DrawItemEventArgs e)

you culd use it as the handler for the DrawItem event.

9:14 AM  
Blogger Luiz Lahamar said...

Sep 17, 2007

Excellent article !!

The comments from John makes it perfect.


8:36 AM  
Blogger geetha said...

Facing on probelm in formdesigner.cs
tabcontrol property only contain OwnerDrawFixed do not contain OwnerDraw so how do i set?

3:11 AM  
Blogger Srinivas said...

Really you have done a good job. Thanks for your suggestion. Its working fine.

4:17 AM  
Blogger Srinivas said...

This comment has been removed by the author.

4:18 AM  
Blogger theory said...

Thanks for this !

when you are disposing, do you also need to dispose of foreBrush:

if (e.Index == this.tabControl1.SelectedIndex)

also it is handy to set references to null after disposal to stop us from using the disposed objects :)

2:41 PM  
Blogger Villain said...

This was very helpful to get me on the right track, however I needed the background image of my form to bleed through. I also needed icons on the tabs. This TabControl draw method works for pulling BackColor or BackgroundImage from the parent control (or form) and showing icons by index or key. It also works for multiple TabControls on one form.

9:55 PM  
Blogger manoj said...

Really it helped me a lot. Thanks buddy.

7:02 AM  
Blogger sonia said...

really good one...thanx a lot

11:34 PM  
Blogger dafeizhu said...

thank u

but the right side of the last tab will still be in gray color...

how do i change that?

7:40 PM  
Blogger Chints said...

I am using .net 2.0 and could'nt find OwnerDraw in DrawMode property. I only have OwnerDrawFixed. I am only getting white background in tabs, while the tab region is brown (default color).

How to fix this.

5:55 AM  
Blogger yogesh said...

This is very helpful
Thanks from the bottom of heart

11:33 PM  
Blogger Muhammad Azeem said...

This is a nice article..
Its very easy to understand ..
And this article is using to learn something about it..

c#,, php tutorial

Thanks a lot..!

10:26 AM  
Blogger Brian said...

This article was very helpful! Didn't know how to do it, and this works. Thanks very much.

9:23 AM  
Blogger Locke said...

Thanks for this. It's exactly what I was looking for! I linked back to this page in my source.

- Locke

12:46 PM  
Blogger Sudhir DBAKings said...

Nice post very helpful


5:09 AM  
Blogger David Carr said...

Nice post - thx. Wondering if anyone has figured out how to achieve the stock 'flat' look that you get in VS2013. Implementing this solution creates a 3D bevel around the control.

2:50 PM  

Post a Comment

<< Home