DrawItem Event

ListBox.DrawItem Event

Occurs when a visual aspect of an owner-drawn ListBox changes.

[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

This event is used by an owner-drawn ListBox. The event is only raised when the DrawMode property is set to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable. You can use this event to perform the tasks needed to draw items in the ListBox. If you have a variable-sized item (when the DrawMode property is set to DrawMode.OwnerDrawVariable), before drawing an item, the MeasureItem event is raised. You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.

For more information about handling events, see Consuming Events.

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 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

ListBox Class | ListBox Members | System.Windows.Forms Namespace | OnDrawItem

Show:
© 2016 Microsoft