Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TabPages - Programmatically setting the font color

Status
Not open for further replies.

denimined

Programmer
Sep 29, 2004
54
CA
Sounds so simple. Probably is, but...
How do I set the caption font color on a TabPage?

Here is a description:
I have a TabControl (System.Windows.Forms.TabControl) with multiple TabPage's. (System.Windows.Forms.TabPage) Each TabPage has its own name (Sales / Inventory / Production). On each TabPage, there are multiple controls. Each control basically consists of a checkbox (this field is relevant) and a text field (here is the value to use).

(The controls are actually more convoluted than that, as there are text or number or date fields available with pick-list buttons and then overall settings to define allow vs require as well as single vs multiple entries - but none of that is relevant to this discussion.)

What I want to have happen is that if any of the controls on a given page are activated, then the caption text for that given tab is red - otherwise it is the normal black. I have all the event handling in place, functioning fine - but I cannot find how to programmatically set the tab font color.

Thank you, thank you, thank you.
[smile2]
 
Try this. I wish I could credit the person I got this from, but I don't remember who it was. Probably somebody in this forum. This code changes the backcolor, forecolor, etc. of the active tab. I think you can probably modify this to fit your needs.
Code:
'''' 1st Part ''''
  ' Tab Control - Tab Color
  Public Sub TabControlColor(ByVal TabCntrl As TabControl, ByVal ee As System.Windows.Forms.DrawItemEventArgs)
    Dim g As Graphics = ee.Graphics
    Dim tp As TabPage = TabCntrl.TabPages(ee.Index)
    Dim br As Brush
    Dim sf As New StringFormat
    Dim r As New RectangleF(ee.Bounds.X, ee.Bounds.Y + 2, ee.Bounds.Width, ee.Bounds.Height - 2)

    'Dim TabTextBrush As Brush = New SolidBrush(Color.White)
    Dim TabTextBrush As Brush = New SolidBrush(Color.Black)
    Dim TabBackBrush As Brush = New SolidBrush(Color.LightBlue)

    sf.Alignment = StringAlignment.Center

    Dim strTitle As String = tp.Text
    'If the current index is the Selected Index, change the color
    If TabCntrl.SelectedIndex = ee.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)

      br = TabBackBrush
      'br = New SolidBrush(Color.PowderBlue)

      'this is the background color of the tab page
      g.FillRectangle(br, ee.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)
      ' I changed to specific color
      br = TabTextBrush
      ' Tried bold, didn't like it
      'Dim ff As Font
      'ff = New Font(TabCntrl.Font, FontStyle.Bold)
      'g.DrawString(strTitle, ff, br, r, sf)
      g.DrawString(strTitle, TabCntrl.Font, br, r, sf)
    Else
      'these are the standard colors for the unselected tab pages
      'br = New SolidBrush(Color.WhiteSmoke)
      br = New SolidBrush(Color.DarkGray)
      g.FillRectangle(br, ee.Bounds)
      br = New SolidBrush(Color.Black)
      g.DrawString(strTitle, TabCntrl.Font, br, r, sf)
    End If
  End Sub

  ''' 2nd Part ''''
  ' Tab Control Color (Just the Tab)
  Private Sub TabControl1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles tabControl1.DrawItem
    TabControlColor(tabControl1, e)
  End Sub
Put the first part in your Main Procedure file or wherever. Put the second part in your form code. This should probably be made into a class but I haven't done it yet. I left some of my testing code in there as comments.


Auguy
Sylvania/Toledo Ohio
 
Oops! Forgot this important piece you also need to add to your form code.
Code:
TabControl1.DrawMode = TabDrawMode.OwnerDrawFixed
Of course change TabControl1 to your TabControl Name.

Auguy
Sylvania/Toledo Ohio
 
Put that last part in the form_load event

Auguy
Sylvania/Toledo Ohio
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top