locked
how to change the color of the tab control in vb.net

    Question

  • can anybody say how change the color of  the tab control in vb.net
    Monday, February 26, 2007 9:38 AM

Answers

  • If you want the entire tabpage to change color then:

    Me.TabPage1.BackColor = Color.Blue

    if you want just the tab to change then you must owner draw the tab...see sample in this thread:

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=839148&SiteID=1

    Monday, February 26, 2007 2:12 PM
  • this will change all tabpages in a tabcontrol to the same color (blue in this sample)

    For Each tp As TabPage In Me.TabControl1.TabPages

    tp.BackColor = Color.Blue

    Next

    Tuesday, February 27, 2007 2:28 PM
  • Excuse me narasiman, but you need to understand that the tabcontrol itself does not have color...it is made up of pages that have color...Without pages it will show as a white rectangle and if you do not want that to show then....set the visible property to false...otherwise to change the color of the tabcontrol...you MUST change the color of the individual tabpages!!!

     

    Friday, March 09, 2007 2:43 PM

All replies

  • If you want the entire tabpage to change color then:

    Me.TabPage1.BackColor = Color.Blue

    if you want just the tab to change then you must owner draw the tab...see sample in this thread:

     

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=839148&SiteID=1

    Monday, February 26, 2007 2:12 PM
  • i am sorry i asked for tab control not tab page
    Tuesday, February 27, 2007 3:20 AM
  • The tab control is made up of tab pages....do you want the entire tab page to change color or just the rectangular tab at the top?
    Tuesday, February 27, 2007 3:35 AM
  • tab control contains a list of tab page is in it i change the color of tab control it show the tabpage1, tabpage2 and so
    Tuesday, February 27, 2007 12:01 PM
  • this will change all tabpages in a tabcontrol to the same color (blue in this sample)

    For Each tp As TabPage In Me.TabControl1.TabPages

    tp.BackColor = Color.Blue

    Next

    Tuesday, February 27, 2007 2:28 PM
  • i was asking tab control and not for tabpage first u understan first
    Friday, March 09, 2007 12:25 PM
  • Excuse me narasiman, but you need to understand that the tabcontrol itself does not have color...it is made up of pages that have color...Without pages it will show as a white rectangle and if you do not want that to show then....set the visible property to false...otherwise to change the color of the tabcontrol...you MUST change the color of the individual tabpages!!!

     

    Friday, March 09, 2007 2:43 PM
  • any updates??
    Tuesday, March 27, 2007 9:39 AM
  • Is there anyway to change the background color of the tab row (where all the tabs are listed) without using OwnerDraw?

    I changed my tabpages background color but it leaves a section above the tabs in the generic color.

    I tried ownerdraw and the results were very discouraging....

    Any ideas?

    Monday, April 02, 2007 8:23 PM
  • Add the following to form load, it redraws any time, the tabs are changed. I'm not sure why.   

     

     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

            TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed

     

        End Sub

     

    I found this Sub somewhere and changed it slightly. Add this sub to your program. It colors the Tab (not the page) the same colors as the background

    of the tab, if it is the one selected. The other tabs are colored a standard white with black text. You can modify it anyway you want, but it seems to work very well for coloring the tabs.

     

        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

                'you could make this a stndard color for the selected page

                br = New SolidBrush(tp.BackColor)

                'this is the background color of the tab page

                g.FillRectangle(br, e.Bounds)

                'this is the background color of the tab page

                'you could make this a stndard color for the selected page

                br = New SolidBrush(tp.ForeColor)

                g.DrawString(strTitle, TabControl1.Font, br, r, sf)

            Else

                'these are the standard colors for the unselected tab pages

                br = New SolidBrush(Color.WhiteSmoke)

                g.FillRectangle(br, e.Bounds)

                br = New SolidBrush(Color.Black)

                g.DrawString(strTitle, TabControl1.Font, br, r, sf)

            End If

     

        End Sub

    • Proposed as answer by zubairTechy Friday, October 29, 2010 2:00 PM
    Thursday, April 05, 2007 4:09 PM
  • Thanks Hillimonster! This works well.
    Wednesday, September 26, 2007 1:11 PM
  • Can someone tell me how to set the tab selected font to bold

    Also is there any way to have gradients on the selected tab ?

    Thanks !
    Monday, April 14, 2008 4:02 PM
  •  

    To get the font to bold, do this:

     

    in the dim statements at the top of the procedure, add this line:

    Code Snippet
    Dim xFont As Font

     

     

     

    Inside the If statement, place this:

    Code Snippet
    xFont = New Font(TabControl1.Font, FontStyle.Bold)

     

     

     

    Inside the Else block, place this instead:

    Code Snippet

     

    xFont = New Font(TabControl1.Font, FontStyle.Regular)

     

     

    then in both the if and the else statements, change the g.DrawString code to this:

     

    Code Snippet

    g.DrawString(strTitle, xFont, br, r, sf)

     

     

     

    That works to set the font to bold on the displayed tab. (I don't know how to do the gradient, though.)

     

    when I use this procedure, it sets the tab button to the correct colors, but it leaves a border around the Tab Control that doesn't match the background color.  It also leaves the background around the tab buttons the default control color.  How do I set those areas to a specific color, so there is nothing left of the original "control" color?

    Wednesday, April 30, 2008 1:10 PM
  •  

    I answered part of my own question. I still don't know how to color the "dead space" to the right of the tab buttons.  Any hints?

     

    Here's how to draw a border around the tabpage:

     

    Code Snippet

    'Place this at the top of the Sub.

    'May need to adjust the + and - numbers to make the border show up correctly.

    Dim rBorder As New RectangleF(TabControl1.Left - 3, TabControl1.Top - 12, SSTab1.Width + 1, SSTab1.Height + 1)

     

     

     

    Code Snippet

    'Place this before the g.fillRectangle(br, r) code:

    g.FillRectangle(br, rBorder)    'Border around entire tabpage area

     

     

     

    Note: I modified the above sub to include both the bold/regular font change and the border.  Since I'm using the same back and fore colors for selected and unselected tab pages, I could've simplified the code even further.  I didn't because I want to have this code to refer back to, should I ever want the tabs to be different colors based on selection.

     

    Here is the complete, revised code.  I used a tab control named SSTab1 instead of TabControl1:

     

    Code Snippet

    Private Sub SSTab1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles SSTab1.DrawItem
            Dim g As Graphics = e.Graphics
            Dim tp As TabPage = SSTab1.TabPages(e.Index)
            Dim brBack As Brush
            Dim brFore As Brush
            Dim sf As New StringFormat
            Dim xFont As Font

            'Set the box border around the tabpages

            '    may need to adjust the + and - amounts.
            Dim rBorder As New RectangleF(SSTab1.Left - 3, SSTab1.Top - 12, _

                SSTab1.Width + 1, SSTab1.Height + 1)
            Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 1, _

                e.Bounds.Width, e.Bounds.Height - 1)
            sf.Alignment = StringAlignment.Center
            Dim strTitle As String = tp.Text
            'If the current index is the Selected Index, change the color
            If SSTab1.SelectedIndex = e.Index Then
                'this is the background color of the tabpage
                'you could make this a standard color for the selected page

                brBack = New SolidBrush(tp.BackColor)   'Set Tab Page Background
                brFore = New SolidBrush(tp.ForeColor)   'Set title foreground
                xFont = New Font(SSTab1.Font, FontStyle.Bold)   'set Font
            Else
                'these are the standard colors for the unselected tab pages
                brBack = New SolidBrush(tp.BackColor)   'Set Tab Page Background
                brFore = New SolidBrush(tp.ForeColor)   'Set title foreground
                xFont = New Font(SSTab1.Font, FontStyle.Regular)   'set Font
            End If

            g.FillRectangle(brFore, rBorder)     'Border around entire tabpage area
            g.FillRectangle(brBack, e.Bounds)     'tab page itself
            g.DrawString(strTitle, xFont, brFore, r, sf)    'Draw the labels.
        End Sub

     

     

     

     

    Wednesday, April 30, 2008 3:04 PM
  • Hi

    How is the sub TabControl1_DrawItem invoked?
    Tuesday, September 09, 2008 12:31 PM
  • I know this a little late, but i had the same issue like Cadius: How to color the "dead space". The tips here helped for the
    tab buttons, but not the "dead space". But i found a solution.

    I theck the State Attribute of the System.Windows.Forms.DrawItemEventArgs. If the state ist DrawItemState.None
    then i paint the Rectangle for the "dead space" else not. Here is my code:

    Private Sub tabWebservice_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles tabWebservice.DrawItem

    Dim g As System.Drawing.Graphics = e.Graphics
    Dim tp As TabPage = tabWebservice.TabPages(e.Index)
    Dim br As System.Drawing.Brush
    Dim colBorder As System.Drawing.Brush
    Dim sf As New System.Drawing.StringFormat
    Dim r As System.Drawing.RectangleF = Nothing

    Dim rBorder As New System.Drawing.RectangleF(tabWebservice.Left, tabWebservice.Top, tabWebservice.Width, tabWebservice.Height)

    sf.Alignment = System.Drawing.StringAlignment.Center

    Dim strTitle As String = tp.Text

    If (e.State = DrawItemState.None) Then
        
    Dim rBack As New System.Drawing.RectangleF(tabWebservice.Left, tabWebservice.Top, tabWebservice.Width, tp.Bounds.Top - 2)'

        
    'this is the background color of the tabpage
        
    'you could make this a stndard color for the selected page
        
    br = New System.Drawing.SolidBrush(tp.BackColor)

        'this is the background color of the tab page
        
    g.FillRectangle(br, rBack)

        br.Dispose()
    End If

    'If the current index is the Selected Index, change the color
    If tabWebservice.SelectedIndex = e.Index Then
        
    r = New System.Drawing.RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

        'this is the background color of the tabpage
        
    'you could make this a stndard color for the selected page
        
    br = New System.Drawing.SolidBrush(tp.BackColor)

        colBorder = New System.Drawing.SolidBrush(System.Drawing.Color.WhiteSmoke)

        'this is the background color of the tab
        
    g.FillRectangle(br, e.Bounds)

        'this is the background color of the tab page
        
    'you could make this a stndard color for the selected page
        
    br.Dispose()

        br = New System.Drawing.SolidBrush(tp.ForeColor)

        r.Offset(0, 1)

        g.DrawString(strTitle, tabWebservice.Font, br, r, sf)

        br.Dispose()

        colBorder.Dispose()

    Else

        'these are the standard colors for the unselected tab pages
        
    br = New System.Drawing.SolidBrush(tp.BackColor)

        r = New System.Drawing.RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height + 2)

        Dim rBack As New System.Drawing.RectangleF(tabWebservice.Left + 1, tabWebservice.Top + e.Bounds.Y + e.Bounds.Height + 3, tabWebservice.Width - 3, tabWebservice.Height - (e.Bounds.Y + e.Bounds.Height + 4))

        'this is the background color of the tab page
        
    g.FillRectangle(br, rBack)

        'this is the background color of the tab button
        g.FillRectangle(br, r)

        br.Dispose()
        br =
    New System.Drawing.SolidBrush(System.Drawing.Color.Black)

        r.Offset(0, 3)

        g.DrawString(strTitle, tabWebservice.Font, br, r, sf)

        br.Dispose()

    End If

    'Dispose objects
    sf.Dispose()

    End Sub

     

    Hope this helps...

     

    Cheers

    • Proposed as answer by Airmax Friday, January 30, 2009 10:16 AM
    Friday, January 30, 2009 10:15 AM
  • The tab control is made up of tab pages....do you want the entire tab page to change color or just the rectangular tab at the top?

    Do you know how to change the rectangular thing at top(the tab or that thing you click on)
    Thursday, March 04, 2010 4:02 PM
  • good one
    Varun Kumar
    Monday, April 19, 2010 1:51 PM
  • Same problem here
    Friday, November 30, 2012 3:21 PM