DrawItemEventArgs Class
Provides data for the DrawItem event.
For a list of all members of this type, see DrawItemEventArgs Members.
System.Object
System.EventArgs
System.Windows.Forms.DrawItemEventArgs
System.Windows.Forms.StatusBarDrawItemEventArgs
[Visual Basic] Public Class DrawItemEventArgs Inherits EventArgs [C#] public class DrawItemEventArgs : EventArgs [C++] public __gc class DrawItemEventArgs : public EventArgs [JScript] public class DrawItemEventArgs extends EventArgs
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
The DrawItem event is raised by owner draw controls, such as ListBox and ComboBox controls. It contains all the information needed for the user to paint the specified item, including the item index, the Rectangle, and the Graphics on which the drawing should be done.
Example
[Visual Basic, C#, C++] The following example demonstrates how to create owner-drawn ListBox items. The code uses the DrawMode property to specify that the items drawn are fixed sized and the DrawItem event to perform the drawing of each item into the ListBox. The example code uses the properties and methods of the DrawItemEventArgs class passed as a parameter to the event handler to draw the items. This example assumes that a ListBox control called listBox1 has been added to a form and that the DrawItem event is handled by the event handler defined in the example code. The example also assumes that items have been added to the ListBox with the text of "Apple", "Orange", and "Plum" in that order.
[Visual Basic] Private Sub listBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem ' Set the DrawMode property to draw fixed sized items. ListBox1.DrawMode = DrawMode.OwnerDrawFixed ' Draw the background of the ListBox control for each item. e.DrawBackground() ' Create a new Brush and initialize to a Black colored brush by default. Dim myBrush As Brush ' Determine the color of the brush to draw each item based on the index of the item to draw. Select Case (e.Index) Case 0 myBrush = Brushes.Red Case 1 myBrush = Brushes.Orange Case 2 myBrush = Brushes.Purple End Select ' Draw the current item text based on the current Font and the custom brush settings. e.Graphics.DrawString(ListBox1.Items(e.Index), e.Font, myBrush, New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) ' If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle() End Sub [C#] private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { // Set the DrawMode property to draw fixed sized items. listBox1.DrawMode = DrawMode.OwnerDrawFixed; // Draw the background of the ListBox control for each item. e.DrawBackground(); // Create a new Brush and initialize to a Black colored brush by default. Brush myBrush = Brushes.Black; // Determine the color of the brush to draw each item based on the index of the item to draw. switch (e.Index) { case 0: myBrush = Brushes.Red; break; case 1: myBrush = Brushes.Orange; break; case 2: myBrush = Brushes.Purple; break; } // Draw the current item text based on the current Font and the custom brush settings. e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush,e.Bounds,StringFormat.GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e.DrawFocusRectangle(); } [C++] private: void listBox1_DrawItem(Object* /*sender*/, System::Windows::Forms::DrawItemEventArgs* e) { // Set the DrawMode property to draw fixed sized items. listBox1->DrawMode = DrawMode::OwnerDrawFixed; // Draw the background of the ListBox control for each item. e->DrawBackground(); // Create a new Brush and initialize to a Black colored brush by default. Brush* myBrush = Brushes::Black; // Determine the color of the brush to draw each item based on the index of the item to draw. switch (e->Index) { case 0: myBrush = Brushes::Red; break; case 1: myBrush = Brushes::Orange; break; case 2: myBrush = Brushes::Purple; break; } // Draw the current item text based on the current Font and the custom brush settings. e->Graphics->DrawString( listBox1->Items->Item[e->Index]->ToString(), e->Font, myBrush, RectangleF::op_Implicit(e->Bounds), StringFormat::GenericDefault); // If the ListBox has focus, draw a focus rectangle around the selected item. e->DrawFocusRectangle(); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Namespace: System.Windows.Forms
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
See Also
DrawItemEventArgs Members | System.Windows.Forms Namespace | DrawItemEventHandler | ComboBox | ListBox | MenuItem | TabControl