开发者

how can i change the background color of tabcontainer tab bar in c#?

i did't find any property of a tab container开发者_StackOverflow to change background color of the bar that contains the tabs how can i do so?


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

Place a panel on the form with the same location and size as the TabControl, put your TabControl inside this panel and set Dock to 'Fill'.

Or the same method in code:

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;
}


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

Make sure you read the first comment, as it contains a correction that will let the code compile.

Borrowed (and corrected) code alert:

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 your form's load event:

tabControl1.DrawItem += ChangeTabColor.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜