Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.70/5 (3 votes)
See more:
When I select the TabPage of TabControl I just want to change the only Header Text Color of Selected Tab in vb.net
Posted
Updated 11-Oct-19 6:31am
Comments
Indivara 25-Jan-11 2:03am    
Google.

Eg:
http://www.tek-tips.com/viewthread.cfm?qid=982090&page=282

Set DrawMode = OwnerDrawFixed [of TabControl]
and use DrawItem to draw the tab headers as shown.
VB
Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem
  Dim tabctl As TabControl = DirectCast(sender, TabControl)
  Dim g As Graphics = e.Graphics
  Dim font As Font = tabctl.Font
  Dim brush As New SolidBrush(Color.Black)
  Dim tabTextArea As RectangleF = RectangleF.op_Implicit(tabctl.GetTabRect(e.Index))
  If tabctl.SelectedIndex = e.Index Then
    font = New Font(font, FontStyle.Bold)
    brush = New SolidBrush(Color.Red)
  End If
  g.DrawString(tabctl.TabPages(e.Index).Text, font, brush, tabTextArea)
End Sub
 
Share this answer
 
Comments
Vasssek 6-Feb-12 13:20pm    
Hmmm, what about tabcontrol for c# compact framework ? There is not DrawMode property...
Great post! I did some modifications by alignment, colors and background color. For give me my bad English. I'm speak Portuguese.

(Otimo post! Fiz algumas modificacoes de alinhamento, cores e cor do fundo. Perdoe-me o meu Ingles ruim. Falo Portugues)

VB
'cor do tabcontrole
    Private Sub TabControl_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ContasTabControl.DrawItem

        Dim tabContas As TabControl = DirectCast(sender, TabControl)
        Dim sTexto As String = tabContas.TabPages(e.Index).Text
        Dim g As Graphics = e.Graphics
        Dim fonte As Font = tabContas.Font
        Dim format = New System.Drawing.StringFormat
'CHANGES HERE...
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim pincel As New SolidBrush(Color.Black)
'RENEMED VARIEBLE HERE...
        Dim retangulo As RectangleF = RectangleF.op_Implicit(tabContas.GetTabRect(e.Index))
        If tabContas.SelectedIndex = e.Index Then
            fonte = New Font(fonte, FontStyle.Bold)
            pincel = New SolidBrush(Color.Black)
'CHANGED BACKGROUN COLOR HERE...
            g.FillRectangle(Brushes.White, retangulo)
        End If
        g.DrawString(sTexto, fonte, pincel, retangulo, format)
    End Sub
 
Share this answer
 
v2
Also, if you want to Bold the font of the selected tab, this might cause it to word-wrap. To fix this, set the control's font to bold so it knows how big to render the tab. Then in your DrawItem method, just set the font back to normal for non-selected tabs.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900