Export (0) Print
Expand All
Expand Minimize

ListViewItemStates Enumeration

Note: This enumeration is new in the .NET Framework version 2.0.

Defines constants that represent the possible states of a ListViewItem.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[FlagsAttribute] 
public enum ListViewItemStates
/** @attribute FlagsAttribute() */ 
public enum ListViewItemStates
FlagsAttribute 
public enum ListViewItemStates

 Member nameDescription
CheckedThe item is checked. 
DefaultThe item is in its default state. 
FocusedThe item has focus. 
GrayedThe item is disabled. 
HotThe item is currently under the mouse pointer. 
IndeterminateThe item is in an indeterminate state. 
MarkedThe item is marked. 
SelectedThe item is selected. 
ShowKeyboardCuesThe item should indicate a keyboard shortcut. 

This enumeration is used by the DrawListViewItemEventArgs.State and DrawListViewSubItemEventArgs.ItemState properties. For more information, see the ListView.DrawItem and ListView.DrawSubItem events.

The following 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 and for the column headers row. A handler for the ListView.DrawSubItem event draws the text values and both the text and background for subitems that have negative values.

A ContextMenu component provides a way to switch between the details view and the list. 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.

For the complete example, see the ListView.OwnerDraw reference topic.

// Draws the backgrounds for entire ListView items.
private void listView1_DrawItem(object sender,
    DrawListViewItemEventArgs e)
{
    if ((e.State & ListViewItemStates.Selected) != 0)
    {
        // Draw the background and focus rectangle for a selected item.
        e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
        e.DrawFocusRectangle();
    }
    else
    {
        // Draw the background for an unselected item.
        using (LinearGradientBrush brush =
            new LinearGradientBrush(e.Bounds, Color.Orange,
            Color.Maroon, LinearGradientMode.Horizontal))
        {
            e.Graphics.FillRectangle(brush, e.Bounds);
        }
    }

    // Draw the item text for views other than the Details view.
    if (listView1.View != View.Details)
    {
        e.DrawText();
    }
}

// Draws the backgrounds for the column header row and for entire
// ListView items.
private void myListView_DrawItem(Object sender, DrawListViewItemEventArgs e)
{
    // Draw the background for the column header row.
    if (e.get_ItemIndex() == -1) {
        e.get_Item().set_BackColor(Color.get_Black());
        e.DrawBackground();
    }
    // Draw the background for a selected item.
    else {
        if (Convert.ToInt32((e.get_State() & ListViewItemStates.Selected))
            != 0) {
            e.get_Graphics().FillRectangle(Brushes.get_Maroon(),
                e.get_Bounds());
            e.DrawFocusRectangle();
        }
        // Draw the background for an unselected item.
        else {
            LinearGradientBrush myBrush = new LinearGradientBrush(
                e.get_Bounds(), Color.get_Orange(), Color.get_Maroon(),
                LinearGradientMode.Horizontal);
            try {
                e.get_Graphics().FillRectangle(myBrush, e.get_Bounds());
            }
            finally {
                myBrush.Dispose();
            }    
        }
    }
    // Draw the item text for views other than the Details view.
    if (!(((ListView)sender).get_View().Equals(View.Details))) {
        e.DrawText();
    }
} //myListView_DrawItem

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft