My Photo
Name:
Location: Bangalore, India
Google

Tips to Reduce Abdominal Fat!

Monday, July 03, 2006

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

Hi,
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);
}
else
{
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
sf.Dispose();
if (e.Index == this.tabControl1.SelectedIndex)
{
TabFont.Dispose();
BackBrush.Dispose();
}
else
{
BackBrush.Dispose();
ForeBrush.Dispose();
}
}

15 Comments:

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.

Thanks.

8:36 AM  
Blogger Unknown 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 Unknown said...

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

4:17 AM  
Blogger Unknown said...

This comment has been removed by the author.

4:18 AM  
Blogger Sean Ryan said...

Thanks for this !

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

if (e.Index == this.tabControl1.SelectedIndex)
{
tabFont.Dispose();
backBrush.Dispose();
foreBrush.Dispose();
}

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

2:41 PM  
Blogger David 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 Unknown said...

Really it helped me a lot. Thanks buddy.

7:02 AM  
Blogger Unknown 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 Chintan 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 bodke said...

This is very helpful
Thanks from the bottom of heart

11:33 PM  
Blogger Unknown 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 Unknown 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