Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DrawListViewItemEventArgs.DrawFocusRectangle Method

Draws a focus rectangle for the ListViewItem if it has focus.

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

public void DrawFocusRectangle()

Use this method to draw a standard focus rectangle around the item. The focus rectangle is normally drawn within the area specified by the Bounds property. If the control is in the details view and the ListView.FullRowSelect property value is false, however, the focus rectangle is drawn around the default text area for the first column of the item.

The focus rectangle is drawn only when the item has focus, so you do not need to check the focus state of the item before you call this method.

The following code example demonstrates how to use the DrawFocusRectangle method in an application that provides custom drawing for a ListView control. In the example, a handler for the ListView.DrawItem event draws the background for entire items. In all views except the details view, this handler also draws the foreground text. In the details view, the foreground text is drawn in the ListView.DrawSubItem event.

For the complete example, see the DrawListViewItemEventArgs overview 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();
    }
}

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft