简体   繁体   中英

How to change Tab Control Background Color (VB.NET)

how can I change the grey part into white color? I want my tabcontrol filled with full white color.

在此处输入图像描述

So far what i did is like this:

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
    Dim g As Graphics = e.Graphics
    Dim tp As TabPage = TabControl1.TabPages(e.Index)
    Dim br As Brush
    Dim sf As New StringFormat

    Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

    sf.Alignment = StringAlignment.Center

    Dim strTitle As String = tp.Text

    'If the current index is the Selected Index, change the color 
    If TabControl1.SelectedIndex = e.Index Then

        'this is the background color of the tabpage header
        br = New SolidBrush(Color.White) ' chnge to your choice
        g.FillRectangle(br, e.Bounds)

        'this is the foreground color of the text in the tab header
        br = New SolidBrush(Color.Black) ' change to your choice
        g.DrawString(strTitle, TabControl1.Font, br, r, sf)

    Else

        'these are the colors for the unselected tab pages 
        br = New SolidBrush(Color.White) ' Change this to your preference
        g.FillRectangle(br, e.Bounds)
        br = New SolidBrush(Color.Black)
        g.DrawString(strTitle, TabControl1.Font, br, r, sf)

    End If
End Sub

and I also put this at PageLoad function:

TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
For Each tg As TabPage In TabControl1.TabPages
    tg.BackColor = Color.White
Next

There is no property to do this. However it is possible by using something like this

http://dotnetrix.co.uk/tabcontrol.htm

All controls on this site are freely available under MIT license.

Spending fair time to research if someone find a solution; There could be some work around. But according to MSDN Ref .

TabControl.BackColor Property

NET Framework (current version) This API supports the product infrastructure and is not intended to be used directly from your code.

This member is not meaningful for this control.

Namespace: System.Windows.Forms Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

As far as I understand, this is adjustable from user's windows setting. ( Highlight Color ) of TabControl , Forms and other controls; otherwise MS could simply turn this property on.

I make a trick to get around this, I put on the gray part a white label and I have the following result:

在此处输入图片说明

Since that color segment of the tabcontrol is unpaintable and cannot be controlled, you have to use a panel or label, etc. to cover up the background color where there is no tabpage header. I use a panel to do this.

This statement working correctly:

Dim PutBackColor As Boolean = False
    Private Sub TabControl1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        If Me.PutBackColor = False Then
            e.Graphics.FillRectangle(New SolidBrush(<YourColor>), TabControl1.Bounds)
            Me.PutBackColor = True
        End If
        e.Graphics.FillRectangle(New SolidBrush(<YourColor>), e.Bounds.X - 2, e.Bounds.Y - 2, e.Bounds.Width + 2, e.Bounds.Height + 2)
        e.Graphics.DrawString(Me.TabControl1.TabPages(e.In  dex).Text, e.Font, Brushes.White, e.Bounds.X + 5, e.Bounds.Y + 5)
        If e.State = DrawItemState.Selected Then
            Me.PutBackColor = False
        End If
 End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM