How can I change the background color of the tab bar of a tabcontainer in c #?

I did not find any tab container property to change the background color of the panel containing the tabs, how can I do this?

+5
source share
2 answers

http://rajeshkm.blogspot.com/2006/07/how-to-change-color-of-tab-control-in.html

Be sure to read the first comment as it contains a fix that will allow you to compile the code.

Borrowed (and adjusted) code warning:

private void ChangeTabColor(object sender, 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();
    }
}

To use this in your code, put this line in the form load event:

tabControl1.DrawItem += ChangeTabColor.
+2
source

The background color of the TabControl is inherited from its parent.

, TabControl, TabControl Dock "Fill".

:

private void Form1_Load(object sender, EventArgs e)
{
    Panel tabBackground = new Panel
    {
        Location = tabControl1.Location,
        Size = tabControl1.Size,
        // Your color here
        BackColor = Color.Magenta
    };
    tabBackground.Controls.Add(tabControl1);
    Controls.Add(tabBackground);
    tabControl1.Dock = DockStyle.Fill;
}
+3

All Articles