Click to See Complete Forum and Search --> : TabControl colored tabs


RD-Printy
July 25th, 2007, 07:28 AM
Has anyone been able to color the actual tab of a tab control? I need to do this in order to match the color of the tab panels and have not been successful as of yet. Any help?

Thanks,
Roger

HanneSThEGreaT
July 25th, 2007, 11:03 AM
OK, you would need to set your TabControl's DrawMode property to OwnerDrawFixed, then override your tabcontrol's DrawItem event.

Something Like :
private void tabControl1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
try
{
Font Myfont; //New Font

Brush Backcolour; //Background Colour

Brush Forecolour; //Foreground Colour

if(e.Index == this.tabControl1.SelectedIndex) //Which Tab Is Selected ¿
{
Myfont = new Font(e.Font,FontStyle.Bold); //Font Style

Backcolour = new System.Drawing.SolidBrush(Color.AliceBlue); //Set Background Colour
Forecolour = Brushes.Crimson; //Set Foreground Colour
}
else
{
Myfont = e.Font; //Restore To Previous Font
Backcolour = new SolidBrush(e.BackColor); //Restore Previous Background Colour
Forecolour = new SolidBrush(e.ForeColor); //Restore Previous Foreground Colour
}

string Tabtext = this.tabControl1.TabPages[e.Index].Text; //Get Text
StringFormat Stringf = new StringFormat(); //Create New String Format Object ( To Center )
Stringf.Alignment = StringAlignment.Center; //Center The Tab Text

e.Graphics.FillRectangle(Backcolour, e.Bounds); //Fill Background

Rectangle Myrect = e.Bounds; //Get Rectangle Bounds ( Where To Draw The Text )
Myrect = new Rectangle(Myrect.X, Myrect.Y + 3, Myrect.Width, Myrect.Height - 3);
e.Graphics.DrawString(Tabtext, Myfont, Forecolour, Myrect, Stringf); //Draw Text With New Font

Stringf.Dispose(); //Dispose String Format Object
if(e.Index == this.tabControl1.SelectedIndex)
{
Myfont.Dispose(); //Dispose Font
Backcolour.Dispose(); //Dispose Background "Brush"
}
else
{
Backcolour.Dispose(); //Dispose Background "Brush"
Forecolour.Dispose(); //Dispose Foreground "Brush"
}
}
catch(Exception Ex) //If Error
{
MessageBox.Show(Ex.Message.ToString()); //Show Error

}

}

I hope it helps!