Joe
Joe

Reputation: 3089

How to change the Font and/or Color of a selected TabPage?

I've been Googling, testing, etc for a couple hours and I'm right where I started off. the WinForms TabControl sucks... Does anyone have tips or code to make it so when I select a TabPage, the font or color changes?

I've messed around with the draw commands and while that does work, it draws the borders/backgrounds so they are very old / outdated looking.

This is basically for a simple tab text editor I'm working on when a TextBox in the control changes I can update the associated tab with either a red font or just bold it to indicate the TextBox on that TabPage is modified.

I've definitely be open for alternative TabControl as long as they are free and come with a VB.Net example.

This is in VB.Net 2008 Express.

Upvotes: 1

Views: 8389

Answers (2)

fivebob
fivebob

Reputation: 710

Set the TabControl drawmode to OwnerDrawFixed then create a eventhandler to paint the tabs in response to the DrawItem event. e.g.

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)
  tabTextArea = 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

Upvotes: 1

Will A
Will A

Reputation: 24988

Set the DrawMode of the tab control to OwnerDrawFixed and paint the tabs yourself in response to the DrawItem event.

Upvotes: 0

Related Questions