Greetings,

I have a tab control and I want to have 1 of the tabs have it's text color changed on a event. I've found answers like C# - TabPage Color event and C# Winform: How to set the Base Color of a TabControl (not the tabpage) but using these sets all colors instead of one.

So I was hoping there is a way to implement this with the tab I wish to change as a method instead of a event?

Something like:

public void SetTabPageHeaderColor(TabPage page, Color color) 
{
    //Text Here
}

If you want to color the tabs, try the following code:

this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl1_DrawItem);

private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
    TabColors[page] = color;
    tabControl1.Invalidate();
}
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
    //e.DrawBackground();
    using (Brush br = new SolidBrush (TabColors[tabControl1.TabPages[e.Index]]))
    {
        e.Graphics.FillRectangle(br, e.Bounds);
        SizeF sz = e.Graphics.MeasureString(tabControl1.TabPages[e.Index].Text, e.Font);
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + (e.Bounds.Width - sz.Width) / 2, e.Bounds.Top + (e.Bounds.Height - sz.Height) / 2 + 1);

        Rectangle rect = e.Bounds;
        rect.Offset(0, 1);
        rect.Inflate(0, -1);
        e.Graphics.DrawRectangle(Pens.DarkGray, rect);
        e.DrawFocusRectangle();
    }
}

For WinForms users reading this - This ONLY works if you set your tab control's DrawMode to OwnerDrawFixed - the DrawItem event never fires if it's set to Normal.


To add to Fun Mun Pieng's answer which works beautifully on Horizontal tabs, if you were to use Vertical tabs (like I was) then you would need something like this:

    private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
    {
        using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
        {
            // Color the Tab Header
            e.Graphics.FillRectangle(br, e.Bounds);
            // swap our height and width dimensions
            var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);

            // Rotate
            e.Graphics.ResetTransform();
            e.Graphics.RotateTransform(-90);

            // Translate to move the rectangle to the correct position.
            e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);

            // Format String
            var drawFormat = new System.Drawing.StringFormat();
            drawFormat.Alignment = StringAlignment.Center;
            drawFormat.LineAlignment = StringAlignment.Center;

            // Draw Header Text
            e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
        }
    }

I will echo the point that ROJO1969 made, if this is in WinForms - then you must set DrawMode to OwnerDrawFixed.

Special thanks goes out to this wonderful blog entry which described how to do a rotation of text on a form.