C# – Change font color on active tabpage

ccolorstabcontrolwinforms

I have a problem with my TabControl. I have various tabs set to be on vertical mode and they are a bit adjusted. Here's a picture of what it looks like:

tabcontrol

And I wonder how I could change from the red to some light blue colour, and maybe change the gray background to a bit lighter.
I tried to follow another guy's advice I found via google on how to change the font to bold and tried this:

InitializeComponent();
tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);

private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
{
    if (e.Index == tabControl1.SelectedIndex)
    {
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text,
            new Font(tabControl1.Font, FontStyle.Bold),
            Brushes.Aqua,
            new PointF(e.Bounds.X + 3, e.Bounds.Y + 3));
    }
    else
    {
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text,
            tabControl1.Font,
            Brushes.Aqua,
            new PointF(e.Bounds.X + 3, e.Bounds.Y + 3));
    }
}

That did not work at all. Nor the color or font was bold or aqua.
Anyone has any ideas how to change it?
For some reason I'm having trouble to change the colors after changing the DrawMode properties to OwnerDrawFixed – which I need to have to be able to use these vertically aligned tabs.

Edit: I do not want to change the font/colors in the actual tabpage, just the tab on the left.

Best Answer

The tabControl1_DrawItem_1 method delivers what you want; the problem with your code is that you are not attaching it to the DrawItem Event. Just replace:

tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);

With:

tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem_1);

CLARIFICATION:

tabControl1_DrawItem_1 assigns the same color to all the tabs (being selected or not). If you want to get a different color for selected/non-selected tabs, you would have to change this in the else part. Sample:

private void tabControl1_DrawItem_1(object sender, DrawItemEventArgs e)
{
    if (e.Index == tabControl1.SelectedIndex)
    {
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text,
            new Font(tabControl1.Font, FontStyle.Bold),
            Brushes.Aqua,
            new PointF(e.Bounds.X + 3, e.Bounds.Y + 3));
    }
    else
    {
        e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text,
            tabControl1.Font,
            Brushes.Black,
            new PointF(e.Bounds.X + 3, e.Bounds.Y + 3));
    }
}
Related Topic