This documentation is archived and is not being maintained.

TabControl.DrawItem Event

Occurs when the tabs are drawn, if the DrawMode property is set to OwnerDrawFixed.

[Visual Basic]
Public Event DrawItem As DrawItemEventHandler
[C#]
public event DrawItemEventHandler DrawItem;
[C++]
public: __event DrawItemEventHandler* DrawItem;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type DrawItemEventArgs containing data related to this event. The following DrawItemEventArgs properties provide information specific to this event.

Property Description
BackColor Gets the background color of the item that is being drawn.
Bounds Gets the rectangle that represents the bounds of the item that is being drawn.
Font Gets the font assigned to the item being drawn.
ForeColor Gets the foreground color of the of the item being drawn.
Graphics Gets the graphics surface to draw the item on.
Index Gets the index value of the item that is being drawn.
State Gets the state of the item being drawn.

Remarks

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, C++] The following example creates a TabControl with one TabPage. This example declares an event handler that is used to draw a string and Rectangle on the tab of tabPage1. The event handler is bound to the DrawItem event.

[Visual Basic, C#, C++] Use the System.Drawing and System.Windows.Forms namespaces for this example.

[Visual Basic] 
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Private tabArea As Rectangle
    Private tabTextArea As RectangleF

    Public Sub New()
        Dim tabControl1 As New TabControl()
        Dim tabPage1 As New TabPage()

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

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

        tabArea = tabControl1.GetTabRect(0)
        tabTextArea = RectangleF.op_Implicit(tabControl1.GetTabRect(0))

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

    ' Declares the event handler DrawOnTab which is a method that
    ' draws a string and 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 font As New Font("Arial", 10.0F)
        Dim brush As New SolidBrush(Color.Red)

        g.DrawRectangle(p, tabArea)
        g.DrawString("tabPage1", font, brush, tabTextArea)
    End Sub

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

[C#] 
using System.Drawing;
using System.Windows.Forms;

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

    public Form1()
    {
        TabControl tabControl1 = new TabControl();
        TabPage tabPage1 = new TabPage();

        // Allows access to the DrawItem event. 
        tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;

        tabControl1.SizeMode = TabSizeMode.Fixed;
        tabControl1.Controls.Add(tabPage1);
        tabControl1.ItemSize = new Size(80, 30);
        tabControl1.Location = new Point(25, 25);
        tabControl1.Size = new Size(250, 250);
        tabPage1.TabIndex = 0;
        ClientSize = new Size(300, 300);
        Controls.Add(tabControl1);

        tabArea = tabControl1.GetTabRect(0);
        tabTextArea = (RectangleF)tabControl1.GetTabRect(0);

        // Binds the event handler DrawOnTab to the DrawItem event 
        // through the DrawItemEventHandler delegate.
        tabControl1.DrawItem += new DrawItemEventHandler(DrawOnTab);
    }

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

        g.DrawRectangle(p, tabArea);
        g.DrawString("tabPage1", font, brush, tabTextArea);
    }

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

[C++] 
using namespace System::Drawing;
using namespace System::Windows::Forms;

public __gc class Form1 : public Form {
private:
    Rectangle  tabArea;
    RectangleF  tabTextArea;

public:
    Form1() {
        TabControl* tabControl1 = new TabControl();
        TabPage* tabPage1 = new TabPage();

        // Allows access to the DrawItem event.
        tabControl1->DrawMode = TabDrawMode::OwnerDrawFixed;

        tabControl1->SizeMode = TabSizeMode::Fixed;
        tabControl1->Controls->Add(tabPage1);
        tabControl1->ItemSize = System::Drawing::Size(80, 30);
        tabControl1->Location = Point(25, 25);
        tabControl1->Size = System::Drawing::Size(250, 250);
        tabPage1->TabIndex = 0;
        ClientSize = System::Drawing::Size(300, 300);
        Controls->Add(tabControl1);

        tabArea = tabControl1->GetTabRect(0);
        tabTextArea = RectangleF::op_Implicit(tabControl1->GetTabRect(0));

        // Binds the event handler DrawOnTab to the DrawItem event
        // through the DrawItemEventHandler delegate.
        tabControl1->DrawItem += new DrawItemEventHandler(this, &Form1::DrawOnTab);
    }

    // Declares the event handler DrawOnTab which is a method that
    // draws a String* and Rectangle on the tabPage1 tab.
private:
    void DrawOnTab(Object* /*sender*/, DrawItemEventArgs* e) {
        Graphics* g = e->Graphics;
        Pen* p = new Pen(Color::Blue);
        System::Drawing::Font* font = new System::Drawing::Font(S"Arial", 10.0f);
        SolidBrush* brush = new SolidBrush(Color::Red);

        g->DrawRectangle(p, tabArea);
        g->DrawString(S"tabPage1", font, brush, tabTextArea);
    }
};

int main() {
    Application::Run(new Form1());
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TabControl Class | TabControl Members | System.Windows.Forms Namespace | DrawItemEventHandler | OnDrawItem

Show: