Thursday, October 06, 2005

Color the Background of a C# TabControl

To set the color of a tabControl to something other than 'Control', set the DrawMode property on the tabControl.
DrawMode = OwnerDrawFixed.

Then, handle the DrawItem event; you will need to draw the tab text yourself, as well as optionally setting the background color.

private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
string tabName = this.tabControl1.TabPages[e.Index].Text;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
Rectangle centerRect = new Rectangle(e.Bounds.X+3,e.Bounds.Y+3,e.Bounds.Width-6,e.Bounds.Height-3);
e.Graphics.FillRectangle(Brushes.WhiteSmoke, centerRect); // colors the tab caption
e.Graphics.DrawString("Filter", new Font("Arial", 9, FontStyle.Regular),System.Drawing.Brushes.Black, centerRect, sf);
} // load


Since this only colors the actual tab captions, you need to color each tab page the same color as here. Do this by setting the BackColor of each tab page to the color you used here.

Note that we only colored an area excluding a 3px border. This is because the tabPages have, by default, a 3px border around them, so our tab captions need to match that.



0 Comments:

Post a Comment

<< Home