Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

DrawListViewItemEventArgs Class

Provides data for the ListView.DrawItem event.


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

Public Class DrawListViewItemEventArgs _
	Inherits EventArgs

The DrawListViewItemEventArgs type exposes the following members.

Public methodDrawListViewItemEventArgsInitializes a new instance of the DrawListViewItemEventArgs class.

Public propertyBoundsGets the size and location of the ListViewItem to draw.
Public propertyDrawDefaultGets or sets a property indicating whether the ListView control will use the default drawing for the ListViewItem.
Public propertyGraphicsGets the Graphics used to draw the ListViewItem.
Public propertyItemGets the ListViewItem to draw.
Public propertyItemIndexGets the index of the ListViewItem within the Items collection of the containing ListView.
Public propertyStateGets the current state of the ListViewItem to draw.

Public methodDrawBackgroundDraws the background of the ListViewItem using its current background color.
Public methodDrawFocusRectangleDraws a focus rectangle for the ListViewItem if it has focus.
Public methodDrawTextDraws the text of the ListViewItem using its current foreground color.
Public methodDrawText(TextFormatFlags)Draws the text of the ListViewItem using its current foreground color and formatting it with the specified TextFormatFlags values.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

The ListView.DrawItem event enables you to customize the appearance of a ListView control using owner drawing.

The ListView.DrawItem event is raised by a ListView control when the ListView.OwnerDraw property is set to true. The DrawListViewItemEventArgs passed to the event handler contains information about the ListViewItem to draw and also provides methods to help you draw the item.

Use the State or Item properties to retrieve information about the item to draw. Use the DrawListViewItemEventArgs.ItemIndex property to retrieve the index of the item.

Use the Graphics property to do the actual drawing within the area specified by the Bounds property. To draw standard ListView elements that do not need customization, use the DrawBackground, DrawText, and DrawFocusRectangle methods.


To avoid issues with graphics flickering when owner drawing, override the ListView control and set the DoubleBuffered property to true. This feature is available only on Windows XP and the Windows Server 2003 family when your application calls the Application.EnableVisualStyles method.

The following code example demonstrates how to provide custom drawing for a ListView control. The ListView control in the example has a gradient background. Subitems with negative values have a red foreground and a black background.

A handler for the ListView.DrawItem event draws the background for entire items. A handler for the ListView.DrawSubItem event draws the text values and both the text and background for subitems that have negative values. A handler for the DrawColumnHeader event draws each column header.

A ContextMenu component provides a way to switch between the details view and the list view. In the list view, only the ListView.DrawItem event is fired. In this case, the text and background are both drawn in the ListView.DrawItem event handler.

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Windows.Forms

Public Class ListViewOwnerDraw
    Inherits Form
    Private WithEvents listView1 As New ListView()
    Private WithEvents contextMenu1 As New ContextMenu()
    Private WithEvents listMenuItem As New MenuItem("List")
    Private WithEvents detailsMenuItem As New MenuItem("Details")

    Public Sub New()

        ' Initialize the shortcut menu. 
        contextMenu1.MenuItems.AddRange(New MenuItem() _
            {Me.listMenuItem, Me.detailsMenuItem})

        ' Initialize the ListView control. 
        With Me.listView1
            .BackColor = Color.Black
            .ForeColor = Color.White
            .Dock = DockStyle.Fill
            .View = View.Details
            .FullRowSelect = True
            .OwnerDraw = True
            .ContextMenu = Me.contextMenu1
        End With 

        ' Add columns to the ListView control. 
        With Me.listView1.Columns
            .Add("Name", 100, HorizontalAlignment.Center)
            .Add("First", 100, HorizontalAlignment.Center)
            .Add("Second", 100, HorizontalAlignment.Center)
            .Add("Third", 100, HorizontalAlignment.Center)
        End With 

        ' Create items and add them to the ListView control. 
        Dim listViewItem1 As New ListViewItem(New String() _
            {"One", "20", "30", "-40"}, -1)
        Dim listViewItem2 As New ListViewItem(New String() _
            {"Two", "-250", "145", "37"}, -1)
        Dim listViewItem3 As New ListViewItem(New String() _
            {"Three", "200", "800", "-1,001"}, -1)
        Dim listViewItem4 As New ListViewItem(New String() _
            {"Four", "not available", "-2", "100"}, -1)
        Me.listView1.Items.AddRange(New ListViewItem() _
            {listViewItem1, listViewItem2, listViewItem3, listViewItem4})

        ' Initialize the form and add the ListView control to it. 
        With Me
            .ClientSize = New Size(450, 150)
            .FormBorderStyle = FormBorderStyle.FixedSingle
            .MaximizeBox = False
            .Text = "ListView OwnerDraw Example"
        End With 

    End Sub 

    ' Clean up any resources being used.         
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
        End If 
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New ListViewOwnerDraw())
    End Sub 

    ' Sets the ListView control to the List view. 
    Private Sub menuItemList_Click(ByVal sender As Object, _
        ByVal e As EventArgs) _
        Handles listMenuItem.Click

        Me.listView1.View = View.List

    End Sub 

    ' Sets the ListView control to the Details view. 
    Private Sub menuItemDetails_Click(ByVal sender As Object, _
        ByVal e As EventArgs) _
        Handles detailsMenuItem.Click

        Me.listView1.View = View.Details

        ' Reset the tag on each item to re-enable the workaround  
        ' in the MouseMove event handler. 
        For Each item As ListViewItem In listView1.Items
            item.Tag = Nothing 

    End Sub 

    ' Selects and focuses an item when it is clicked anywhere along  
    ' its width. The click must normally be on the parent item text. 
    Private Sub listView1_MouseUp(ByVal sender As Object, _
        ByVal e As MouseEventArgs) _
        Handles listView1.MouseUp

        Dim clickedItem As ListViewItem = Me.listView1.GetItemAt(5, e.Y)
        If (clickedItem IsNot Nothing) Then
            clickedItem.Selected = True
            clickedItem.Focused = True 
        End If 

    End Sub 

    ' Draws the backgrounds for entire ListView items. 
    Private Sub listView1_DrawItem(ByVal sender As Object, _
        ByVal e As DrawListViewItemEventArgs) _
        Handles listView1.DrawItem

        If Not (e.State And ListViewItemStates.Selected) = 0 Then 

            ' Draw the background for a selected item.
            e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)


            ' Draw the background for an unselected item. 
            Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
                Color.Maroon, LinearGradientMode.Horizontal)
                e.Graphics.FillRectangle(brush, e.Bounds)
            End Try 

        End If 

        ' Draw the item text for views other than the Details view. 
        If Not Me.listView1.View = View.Details Then
        End If 

    End Sub 

    ' Draws subitem text and applies content-based formatting. 
    Private Sub listView1_DrawSubItem(ByVal sender As Object, _
        ByVal e As DrawListViewSubItemEventArgs) _
        Handles listView1.DrawSubItem

        Dim flags As TextFormatFlags = TextFormatFlags.Left

        Dim sf As New StringFormat()

            ' Store the column text alignment, letting it default 
            ' to Left if it has not been set to Center or Right. 
            Select Case e.Header.TextAlign
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                    flags = TextFormatFlags.HorizontalCenter
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
                    flags = TextFormatFlags.Right
            End Select 

            ' Draw the text and background for a subitem with a  
            ' negative value.  
            Dim subItemValue As Double 
            If e.ColumnIndex > 0 AndAlso _
                Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                subItemValue < 0 Then 

                ' Unless the item is selected, draw the standard  
                ' background to make it stand out from the gradient. 
                If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                End If 

                ' Draw the subitem text in red to highlight it. 
                e.Graphics.DrawString(e.SubItem.Text, _
                    Me.listView1.Font, Brushes.Red, e.Bounds, sf)


            End If 

            ' Draw normal text for a subitem with a nonnegative  
            ' or nonnumerical value.

        End Try 

    End Sub 

    ' Draws column headers. 
    Private Sub listView1_DrawColumnHeader(ByVal sender As Object, _
        ByVal e As DrawListViewColumnHeaderEventArgs) _
        Handles listView1.DrawColumnHeader

        Dim sf As New StringFormat()

            ' Store the column text alignment, letting it default 
            ' to Left if it has not been set to Center or Right. 
            Select Case e.Header.TextAlign
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
            End Select 

            ' Draw the standard header background.

            ' Draw the header text. 
            Dim headerFont As New Font("Helvetica", 10, FontStyle.Bold)
                e.Graphics.DrawString(e.Header.Text, headerFont, _
                    Brushes.Black, e.Bounds, sf)
            End Try 

        End Try 

    End Sub 

    ' Forces each row to repaint itself the first time the mouse moves over  
    ' it, compensating for an extra DrawItem event sent by the wrapped  
    ' Win32 control. 
    Private Sub listView1_MouseMove(ByVal sender As Object, _
        ByVal e As MouseEventArgs) _
        Handles listView1.MouseMove

        Dim item As ListViewItem = listView1.GetItemAt(e.X, e.Y)
        If item IsNot Nothing AndAlso item.Tag Is Nothing Then
            item.Tag = "tagged" 
        End If 

    End Sub 

    ' Resets the item tags.  
    Private Sub listView1_Invalidated(ByVal sender As Object, _
        ByVal e As InvalidateEventArgs) Handles listView1.Invalidated

        For Each item As ListViewItem In listView1.Items
            If item Is Nothing Then Return
            item.Tag = Nothing 

    End Sub 

    ' Forces the entire control to repaint if a column width is changed. 
    Private Sub listView1_ColumnWidthChanged(ByVal sender As Object, _
        ByVal e As ColumnWidthChangedEventArgs) Handles listView1.ColumnWidthChanged


    End Sub 

End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2015 Microsoft