0

While trying to change the color of the tab based on button click event I came across this page:

Set TabPage Header Color

It appears to work, however I lost all the other formatting of my tabs and they now appear blank after I set the DrawMode=OwnerDrawFixed. How do I set the color of the tab of a specific page and still show the tabs as normal?

Code:

private void button3_Click(object sender, EventArgs e)
{
    this.TabControlMain.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.TabControlMain_DrawItem);
    SetTabHeader (tabDownload, System.Drawing.Color.Green);
}
private Dictionary<TabPage, Color> TabColors = new Dictionary<TabPage, Color>();
private void SetTabHeader(TabPage page, Color color)
{
    TabColors[page] = color;
    tabDownload.Invalidate();
}

private void TabControlMain_DrawItem(object sender, DrawItemEventArgs e)
{
    //e.DrawBackground();
    using (Brush br = new SolidBrush(TabColors[TabControlMain.TabPages[e.Index]]))
    {
        e.Graphics.FillRectangle(br, e.Bounds);
        SizeF sz = e.Graphics.MeasureString(TabControlMain.TabPages[e.Index].Text, e.Font);
        e.Graphics.DrawString(TabControlMain.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();
    }
}

Below is a screenshotScreenshot

Community
  • 1
  • 1
Cade
  • 97
  • 2
  • 8

2 Answers2

0

Use Y(et)A(nother)TabControl

Gives you the possibly to draw own custom headers:

TabControl

TabControl

public override void DrawTab( Color foreColor,
                              Color backColor,
                              Color highlightColor,
                              Color shadowColor,
                              Color borderColor,
                              bool active,
                              bool mouseOver,
                              DockStyle dock,
                              Graphics graphics,
                              SizeF tabSize )
{
  if( active )
  {
    Pen p = new Pen( borderColor );
    graphics.DrawRectangle( p, 0, 0, tabSize.Width, tabSize.Height );
    p.Dispose();
  }
  else
  {
    Brush b = Brushes.Peru;
    float dif = tabSize.Height / 4.0f;
    RectangleF r = new RectangleF( 0.0f, dif, 
         tabSize.Width, tabSize.Height - dif - dif );
    graphics.FillRectangle( b, r );
  }
}
Sievajet
  • 3,443
  • 2
  • 18
  • 22
0

In principle your code works, if not fine then at least sufficiently.

But you need to fix a few simple issues:

  • Of course, if your Tab is OwnerDrawn you need to do it from the start, not just after pressing the Button! So hook up the event right from the start, best in the Property-Event Tab so that it is placed in the Desgner.cs file where it belongs.

Now the Tabs are drawn and their Label texts show.

  • If you want to change a color you need to Invalidate the Tab Control, not the TabPage!

so change

tabDownload.Invalidate();

to

TabControlMain.Invalidate();

To make the example complete I have done only this:

private void Form1_Load(object sender, EventArgs e)
{
    foreach (TabPage tp in TabControlMain.TabPages)
            TabColors.Add(tp, tp.BackColor);
}

This sets all Tab colors in the Dictionary to the Colors of the Pages.

  • The code to draw the FocusRectangle could be improved.

First set the Padding like this:

this.TabControlMain.Padding = new System.Drawing.Point(10, 4);

This gives us enough room to draw a nice FocusRectangle..

Then change the code to draw it only on the selected Tab, maybe like this:

    if (TabControlMain.SelectedIndex == e.Index)
        using (Pen pen = new Pen(Color.Gray))
        {
            pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            Rectangle rect = e.Bounds;
            rect.Offset(0, 1);
            rect.Inflate(-3,-2);
            e.Graphics.DrawRectangle(pen, rect);
        }

Now it looks like this:

enter image description here

Of course managing the colors is up to you..

TaW
  • 53,122
  • 8
  • 69
  • 111