.
is drawn on the Windows Form. When the
event. Therefore, if the
.
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("Arial", 10.0f);
Font font1 = new Font("Comic Sans MS", 12.0f);
SolidBrush brush = new SolidBrush(Color.Blue);
SolidBrush brush1 = new SolidBrush(Color.Red );
g.DrawRectangle(p, tabArea1);
g.DrawRectangle (p1,tabArea);
g.DrawString("tabPage1", font, brush, tabTextArea);
g.DrawString("tabPage2", 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("Arial", 10.0F)
Dim font1 As New Font("Comic Sans MS", 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("tabPage2", font, brush, tabTextArea)
g.DrawString("tabPage1", font1, brush1, tabTextArea1)
End Sub
End Class