.NET Framework Class Library
ListBox..::.DrawItem Event

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

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

Visual Basic (Declaration)
Public Event DrawItem As DrawItemEventHandler
Visual Basic (Usage)
Dim instance As ListBox
Dim handler As DrawItemEventHandler

AddHandler instance.DrawItem, handler
C#
public event DrawItemEventHandler DrawItem
Visual C++
public:
 event DrawItemEventHandler^ DrawItem {
    void add (DrawItemEventHandler^ value);
    void remove (DrawItemEventHandler^ value);
}
JScript
JScript does not support events.
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.

Examples

The following code 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 requires 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 requires that items have been added to the ListBox with the text of "Apple", "Orange", and "Plum" in that order.

Visual Basic
Private WithEvents ListBox1 As New ListBox()

Private Sub InitializeListBox() 
    ListBox1.Items.AddRange(New Object() _
        {"Red Item", "Orange Item", "Purple Item"})
    ListBox1.Location = New System.Drawing.Point(81, 69)
    ListBox1.Size = New System.Drawing.Size(120, 95)
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed
    Controls.Add(ListBox1)

End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DrawItemEventArgs) _
 Handles ListBox1.DrawItem

    ' Draw the background of the ListBox control for each item.
    e.DrawBackground()

    ' Define the default color of the brush as black.
    Dim myBrush As Brush = Brushes.Black

    ' 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).ToString(), _
        e.Font, myBrush, e.Bounds, StringFormat.GenericDefault)

    ' If the ListBox has focus, draw a focus rectangle around  _ 
    ' the selected item.
    e.DrawFocusRectangle()
End Sub
C#
private ListBox ListBox1 = new ListBox();
private void InitializeListBox()
{
    ListBox1.Items.AddRange(new Object[] 
        { "Red Item", "Orange Item", "Purple Item" });
    ListBox1.Location = new System.Drawing.Point(81, 69);
    ListBox1.Size = new System.Drawing.Size(120, 95);
    ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    ListBox1.DrawItem += new DrawItemEventHandler(ListBox1_DrawItem);
    Controls.Add(ListBox1);
}

private void ListBox1_DrawItem(object sender, 
    System.Windows.Forms.DrawItemEventArgs e)
{
    // Draw the background of the ListBox control for each item.
    e.DrawBackground();
    // Define the default color of the brush as black.
    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();
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Community Content

rasikaw
Question
Hi,

I am trying to do something like this within ListBox.DrawItem:

e.DrawBackground();


// Draw an icon image.

e.Graphics.DrawImage(myImage, 0, 0, myImage.Width, myImage.Height);


// Alternate row color.

if (e.Index % 2 == 0)

{

Brush bgBrush = new SolidBrush(Color.LightGray);

e.Graphics.FillRectangle(bgBrush, 0, 0, e.Bounds.Width, e.Bounds.Height);

bgBrush.Dispose();

}



However the drawing of the image and alternating of the row color only works on the very first item displayed on the very top of the ListBox, not the items below it. If I scroll up continously however (on a ListBox with a lot of items in it) the image begins to get painted on the list items and the background color begins alternate on the items.


Any ideas on how to resolve this???


Schmook007
re: Question
You're basically redrawing the first items rectangle over and over again every time a drawItem event is fired...

Chanage this:
e.Graphics.FillRectangle(bgBrush, 0, 0, e.Bounds.Width, e.Bounds.Height);

to this:
e.Graphics.FillRectangle(bgBrush, e.Bounds.X, e.Bound.Y, e.Bounds.Width, e.Bounds.Height);

Tags :

Rootzle
Items not appearing selected.
This works great for me and is exactly what I want. I am drawing lines to associate items in two separate listboxes and this makes things a LOT easier to read. There is only one caveat: The items with a different backcolor do not appear to be selected. They are (demonstrably) selected, but it is a little disconcerting not to be able to SEE the items being selected. I think I am going to play with redrawing the control and changing the color of the selected item to red or something. If you all have any better ideas, I am open to them.
Tags :

Kratz
Re: Items not appearing selected.

To mimic the default behavior of the list box, add the following code which changes the brush color to white when the object is selected instead of black. Otherwise the dark blue selection rectangle makes black text difficult to read.

If (e.State And Windows.Forms.DrawItemState.Selected) = Windows.Forms.DrawItemState.Selected Then
myBrush = Drawing.Brushes.White
End If

e.DrawFocusRectangle()

Tags :

Page view tracker