TabControl BackColor not SystemColor.Control

Jarod

Regular
Joined
Feb 5, 2003
Messages
82
Location
Bruxelles
Is there a simply way to avoid the standard "BackColor" of the TabControl?
I mean, if you put on a form a TabControl with one TabPage. Set the BackColor of the form and of the TabPage to, say, Blue.
You will still have the "tab zone" of the TabControl to be colored by SystemColor.Control
Can we change that? Is there a property somewhere I missed?
Do we have to do that manually using GDI methods? (if so, does somebody has some code or tricks?)

Thanks,
 
Hello Heiko

I hadn't time to spend to many time on that. But what I have seen is that it is not a bug (dixit Microsoft Team) and this is the normal working, BackColor attribute not used for the TabControl

I have so tried to paint it myself (like a barbarian, i'm not very used with the GDI+ for the moment and don't have time to investigate in that domain for the moment.
Unfortunatlt, it doesn't work fine. I have tried to inherit from teh control and to paint that part in every painting functions. But this is not done on the very first display of the control.
And moreover this paint is not so easy if we want to be really efficient (when a tab is selected, a small "grey line" is visible at the top of the tab)

A tricky solution (that I don't like :-D) is to add panel on the form and make it the exact size to hide that part. But the result is not really nice for me (same remark as for the painting)

I have found a control that is solving that.
But this control is implemented on a panel, drawing everything, any tab, ... and the result seems not to be very "TabControl like" to me.
However, here is the address :
http://www.dotnetmagic.com
A new version has been released since I have tested it.


I am really searching to the TabControl as it exist now but without that grayed Tab Zone

If you see something... :-)
 
Hi,

Has anyone had anymore luck with the tabcontrol. I just want to be able to change the color of the tabs themselves.

Is GDI what I need to be looking for to modify and customize my controls? Has anyone used the control that Jarod posted to know if it is any good?

Just trying to figure out where I need to start.

Thanks
 
How to color the tabs themselves:

First, set the DrawMode of the TabControl to OwnerDrawFixed in properties, or add this line to form_load:
Visual Basic:
Me.TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed

Then add this code in for the drawItem event that will now fire for the TabControl (hack away at this to get a result you like):
Visual Basic:
    Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) _
Handles TabControl1.DrawItem

        e.Graphics.FillRectangle(Brushes.LightGoldenrodYellow, e.Bounds)
        e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y)

End Sub
 
Changing the Tab Control Background Color In .NET

I haven't found a way to actually change the back ground color but I've done the next best thing. This code only works with a single line tab control. Consider the following code...
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Lavender),
this.GetTabRect(this.TabCount -1).Right,e.Bounds.Top,
this.ClientRectangle.Right-this.GetTabRect(this.TabCount -1).Right,
e.Bounds.Bottom - e.Bounds.Top);

e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Gray)),
this.ClientRectangle.Right-1,
this.ClientRectangle.Top,
this.ClientRectangle.Right-1,
e.Bounds.Bottom);

e.Graphics.DrawLine(new Pen(new SolidBrush(Color.White)),
this.ClientRectangle.Left,
this.ClientRectangle.Top,
this.ClientRectangle.Right,
this.ClientRectangle.Top);

e.Graphics.FillRectangle(new SolidBrush(Color.LightSteelBlue),
e.Bounds.Left - 1,
e.Bounds.Top, e.Bounds.Width +1, e.Bounds.Height);

SizeF stringSize = new SizeF();

stringSize = e.Graphics.MeasureString(this.TabPages[e.Index].Text,
e.Font,e.Bounds.Width,
StringFormat.GenericDefault);

float CenterX = (e.Bounds.Width - stringSize.Width)/2;

float CenterY = (e.Bounds.Height - stringSize.Height)/2;

e.Graphics.DrawString(this.TabPages[e.Index].Text,
e.Font, new SolidBrush(Color.Black), e.Bounds.Left + CenterX,
e.Bounds.Top + CenterY+1,
StringFormat.GenericDefault);

base.OnDrawItem (e);
}

This is an override for a custom class derived from the TabControl class but you should be able to modify the code for an event handler. You need to remember to set the DrawMode to OwnerDrawFixed. This code first uses the GetTabRect function to find the Rectangle of the last tab and use the right side of it as the left side of the fill rectangle. Then the code draws two lines to make it look 3D. The rest of the code draws the text back on the middle of the tabs.
 
I have been able to use some of the theory and code here and elsewhere to expand this and allow bleed through of background image and color and inclusion of icons. I have posted about it here including the full code for the reusable method. i hope this is helpful.
 
Just had the same problem myself, came up with the following which produces reasonable results :

Code:
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        Dim sf As New StringFormat, tmpRect As New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        If TabControl1.SelectedIndex = e.Index Then
            tmpRect.Height -= 1
        Else
            tmpRect.Height += 2
        End If
        If (e.Index = 0 And TabControl1.SelectedIndex <> 0) Or (e.Index = 1 And TabControl1.SelectedIndex = 0) Then e.Graphics.FillRectangle(Brushes.Black, New Rectangle(0, 0, TabControl1.Width, TabControl1.Height))
        e.Graphics.FillRectangle(Brushes.Black, tmpRect)
        e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, Brushes.Black, tmpRect, sf)
    End Sub
 
Back
Top