how can i add color to the TabControl..?
we can add color to the TabPage but how to Tab control.

Recommended Answers

All 7 Replies

I tested the code and as far as I can tell it does what you want it to do.

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

        'Firstly we'll define some parameters.
        Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
        Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
        Dim FillBrush As New SolidBrush(Color.Red)
        Dim TextBrush As New SolidBrush(Color.White)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        'If we are currently painting the Selected TabItem we'll 
        'change the brush colors and inflate the rectangle.
        If CBool(e.State And DrawItemState.Selected) Then
            FillBrush.Color = Color.White
            TextBrush.Color = Color.Red
            ItemRect.Inflate(2, 2)
        End If

        'Set up rotation for left and right aligned tabs
        If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
            Dim RotateAngle As Single = 90
            If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
            Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
            e.Graphics.TranslateTransform(cp.X, cp.Y)
            e.Graphics.RotateTransform(RotateAngle)
            ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
        End If

        'Next we'll paint the TabItem with our Fill Brush
        e.Graphics.FillRectangle(FillBrush, ItemRect)

        'Now draw the text.
        e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)

        'Reset any Graphics rotation
        e.Graphics.ResetTransform()

        'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()
End Sub

Hope this actually does what you want it to do.

Originally posted by user "KEBO" on vbForums

Hi,

To let the code that Animal Mother provided work.
You need to change in the properties of that Tabcontrol the Drawmode = OwnerDrawFixed

Yip, that is true Sorry about that. :confused:

Thanx Luc001

Yip, that is true Sorry about that. :confused:

Thanx Luc001

thanks animal mother. it paints tabpage header. but it doesnt paint remaining part of the tab control... is ther any solution to this..

Member Avatar for Unhnd_Exception

You may have to create your own. Its no small task though.

I started one that will do what you want but not finished enough to give.

Heres a jerry rigged solution.

It won't paint the tab header put will add color to the body. Don't know if I'd use it.

Mask the tabcontrol with a picture box and sets its region to exclude the tab page area.

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

        Dim PictureBox1 As New PictureBox
        PictureBox1.BackColor = Color.Purple
        PictureBox1.BorderStyle = BorderStyle.FixedSingle
        PictureBox1.Size = TabControl1.ClientSize - New Size(0, TabControl1.ItemSize.Height)
        PictureBox1.Location = Me.PointToClient(TabControl1.PointToScreen(TabControl1.ClientRectangle.Location)) + New Size(0, TabControl1.ItemSize.Height)

        Dim TabRegion As New Region(New Rectangle(0, 0, PictureBox1.Width, PictureBox1.Height))
        TabRegion.Exclude(New Rectangle(TabControl1.DisplayRectangle.X, TabControl1.DisplayRectangle.Y - TabControl1.ItemSize.Height, TabControl1.DisplayRectangle.Width, TabControl1.DisplayRectangle.Height))

        PictureBox1.Region = TabRegion

        Me.Controls.Add(PictureBox1)
        PictureBox1.BringToFront()

    End Sub

thanks animal mother. it paints tabpage header. but it doesnt paint remaining part of the tab control... is ther any solution to this..

Hi Ashu26,

You can do it like this;

Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
        'Firstly we'll define some parameters.
        Dim CurrentTab As TabPage = TabControl1.TabPages(e.Index)
        Dim ItemRect As Rectangle = TabControl1.GetTabRect(e.Index)
        Dim FillBrush As New SolidBrush(Color.RoyalBlue)
        Dim TextBrush As New SolidBrush(Color.White)
        Dim sf As New StringFormat
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center

        'If we are currently painting the Selected TabItem we'll 
        'change the brush colors and inflate the rectangle.
        If CBool(e.State And DrawItemState.Selected) Then
            FillBrush.Color = Color.White
            TextBrush.Color = Color.RoyalBlue
            TabPage1.BackColor = Color.LightBlue  ' change the backcloor of your tabpage1
            TabPage2.BackColor = Color.LightGreen ' Change backcolor of tabpage2
            ItemRect.Inflate(2, 2)
        End If

        'Set up rotation for left and right aligned tabs
        If TabControl1.Alignment = TabAlignment.Left Or TabControl1.Alignment = TabAlignment.Right Then
            Dim RotateAngle As Single = 90
            If TabControl1.Alignment = TabAlignment.Left Then RotateAngle = 270
            Dim cp As New PointF(ItemRect.Left + (ItemRect.Width \ 2), ItemRect.Top + (ItemRect.Height \ 2))
            e.Graphics.TranslateTransform(cp.X, cp.Y)
            e.Graphics.RotateTransform(RotateAngle)
            ItemRect = New Rectangle(-(ItemRect.Height \ 2), -(ItemRect.Width \ 2), ItemRect.Height, ItemRect.Width)
        End If

        'Next we'll paint the TabItem with our Fill Brush
        e.Graphics.FillRectangle(FillBrush, ItemRect)

        'Now draw the text.
        e.Graphics.DrawString(CurrentTab.Text, e.Font, TextBrush, RectangleF.op_Implicit(ItemRect), sf)

        'Reset any Graphics rotation
        e.Graphics.ResetTransform()

        'Finally, we should Dispose of our brushes.
        FillBrush.Dispose()
        TextBrush.Dispose()

    End Sub
Member Avatar for Unhnd_Exception

That still doesn't paint the tab control, only headers and sets the tab pages to a backcolor. To paint the tab control itself you will have to create your own.

I would like to see some code that paints the actual body of the control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.