Microsoft KB Archive/814345

= BUG: Font of TabPage text does not change when you change the Font property of the TabPage control =

Article ID: 814345

Article Last Modified on 5/21/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-



SYMPTOMS
When you set the Font property of a TabPage control in a TabControl control, the font of the TabPage text does not change. Only the fonts of the controls that appear on the TabPage change.



CAUSE
This problem occurs because the DrawMode property of a TabControl is set to Normal.

The DrawItem event determines the way that the TabControl is drawn on the Windows Form. When the DrawMode property is set to Normal (the default setting), you cannot gain access to the DrawItem event. Therefore, if the DrawMode property is set to Normal, you cannot change the fonts of TabPage controls that are in a TabControl. 

RESOLUTION
To resolve this problem, set the DrawMode property of the TabControl to OwnerDrawFixed, and then declare an event handler that is bound to the DrawItem event. This event handler draws the required font for each tab. The following sample code demonstrates how to do this.

Visual C# Sample Code
using System.Drawing; using System.Windows.Forms;

public class Form1 : Form {   private Rectangle tabArea ,tabArea1; private RectangleF tabTextArea, tabTextArea1;

public Form1 {       TabControl MytabControl1 = new TabControl; TabPage MytabPage1 = new TabPage; TabPage MytabPage2 = new TabPage;

// Allows access to the DrawItem event. MytabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; MytabControl1.SizeMode = TabSizeMode.Fixed;

MytabControl1.Controls.Add(MytabPage1); MytabControl1.Controls.Add(MytabPage2); MytabControl1.ItemSize = new Size(80, 30); MytabControl1.Location = new Point(25, 25); MytabControl1.Size = new Size(250, 250); MytabPage1.TabIndex = 0; MytabPage2.TabIndex = 1; ClientSize = new Size(300, 300); Controls.Add(MytabControl1);

tabArea = MytabControl1.GetTabRect(0); tabArea1= MytabControl1.GetTabRect(1); tabTextArea = (RectangleF)MytabControl1.GetTabRect(0); tabTextArea1 = (RectangleF)MytabControl1.GetTabRect(1); // Binds the event handler DrawOnTab to the DrawItem event // through the DrawItemEventHandler delegate. MytabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab); }

// Declares the event handler DrawOnTab. DrawOnTab is a method that // draws a string and a Rectangle on the tabPage1 tab. private void DrawOnTab(object sender, DrawItemEventArgs e)   { Graphics g = e.Graphics; Pen p = new Pen(Color.Blue); Pen p1 = new Pen(Color.Red ); Font font = new Font(&quot;Arial&quot;, 10.0f); Font font1 = new Font(&quot;Comic Sans MS&quot;, 12.0f); SolidBrush brush = new SolidBrush(Color.Blue); SolidBrush brush1 = new SolidBrush(Color.Red );

g.DrawRectangle(p, tabArea1); g.DrawRectangle (p1,tabArea); g.DrawString(&quot;tabPage1&quot;, font, brush, tabTextArea); g.DrawString(&quot;tabPage2&quot;, font1, brush1, tabTextArea1);

}

static void Main {       Application.Run(new Form1); } }

Visual Basic .NET Sample Code
Imports System.Drawing Imports System.Windows.Forms

Public Class Form1 Inherits Form

Private tabArea, tabArea1 As Rectangle Private tabTextArea, tabTextArea1 As RectangleF

Public Sub New Dim MytabControl1 As New TabControl Dim MytabPage As New TabPage Dim Mytabpage1 As New TabPage

' Allows access to the DrawItem event. MytabControl1.DrawMode = TabDrawMode.OwnerDrawFixed

MytabControl1.SizeMode = TabSizeMode.Fixed MytabControl1.Controls.Add(MytabPage) MytabControl1.Controls.Add(MytabPage1) MytabControl1.ItemSize = New Size(80, 30) MytabControl1.Location = New Point(25, 25) MytabControl1.Size = New Size(250, 250) MytabPage1.TabIndex = 0 ClientSize = New Size(300, 300) Controls.Add(MytabControl1)

tabArea = MytabControl1.GetTabRect(0) tabArea1 = MytabControl1.GetTabRect(1)

tabTextArea = RectangleF.op_Implicit(MytabControl1.GetTabRect(0)) tabTextArea = RectangleF.op_Implicit(MytabControl1.GetTabRect(1))

' Binds the event handler DrawOnTab to the DrawItem event ' through the DrawItemEventHandler delegate. AddHandler MytabControl1.DrawItem, AddressOf DrawOnTab End Sub

' Declares the event handler DrawOnTab. DrawOnTab is a method that ' draws a string and a Rectangle on the tabPage1 tab. Private Sub DrawOnTab(ByVal sender As Object, ByVal e As DrawItemEventArgs) Dim g As Graphics = e.Graphics

Dim p As New Pen(Color.Blue) Dim p1 As New Pen(Color.Red)

Dim font As New Font(&quot;Arial&quot;, 10.0F) Dim font1 As New Font(&quot;Comic Sans MS&quot;, 11.0F)

Dim brush As New SolidBrush(Color.Blue) Dim brush1 As New SolidBrush(Color.Red)

g.DrawRectangle(p, tabArea) g.DrawRectangle(p1, tabArea1) g.DrawString(&quot;tabPage2&quot;, font, brush, tabTextArea) g.DrawString(&quot;tabPage1&quot;, font1, brush1, tabTextArea1) End Sub

End Class



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.

