TabControl::DrawItem Event


Occurs when the TabControl needs to paint each of its tabs if the DrawMode property is set to OwnerDrawFixed.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

event DrawItemEventHandler^ DrawItem {
	void add(DrawItemEventHandler^ value);
	void remove(DrawItemEventHandler^ value);

When you set the DrawMode property to OwnerDrawFixed, the TabControl raises the DrawItem event whenever it needs to paint one of its tabs. To customize the appearance of the tabs, provide your own painting code in a handler for the DrawItem event.

The TabControl does not support variable tab sizes with owner drawing.

For more information about handling events, see Handling and Raising Events.

The following code 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.

Use the System.Drawing and System.Windows.Forms namespaces for this example.

using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public Form
   Rectangle tabArea;
   RectangleF tabTextArea;

      TabControl^ tabControl1 = gcnew TabControl;
      TabPage^ tabPage1 = gcnew 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 = tabControl1->GetTabRect( 0 );

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


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


int main()
   Application::Run( gcnew Form1 );

.NET Framework
Available since 1.1
Return to top