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

ListViewInsertionMark.Index Property

Gets or sets the index of the item next to which the insertion mark appears.

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

public int Index { get; set; }

Property Value

Type: System.Int32
The index of the item next to which the insertion mark appears or -1 when the insertion mark is hidden.

The insertion mark appears to the left of the item with the specified index if the AppearsAfterItem property is set to false; otherwise, it appears to the right of the item. The insertion mark disappears automatically when a dragged item is inserted into the ListView.Items collection. To remove the insertion mark manually, for example, when an item is dragged onto itself or dragged outside the control, set the Index value to -1.

Use the NearestIndex method to retrieve the index of the item closest to a specified location, such as the location of the mouse pointer when positioning an item in a drag-and-drop operation.

The following code example demonstrates how to use the ListView insertion mark feature and implements drag-and-drop item reordering using the standard drag events. The position of the insertion mark is updated in a handler for the Control.DragOver event. In this handler, the position of the mouse pointer is compared to the midpoint of the nearest item, and the result is used to determine whether the insertion mark appears to the left or the right of the item.

For the complete example, see the ListViewInsertionMark overview reference topic.

// Moves the insertion mark as the item is dragged. 
private void myListView_DragOver(object sender, DragEventArgs e)
    // Retrieve the client coordinates of the mouse pointer.
    Point targetPoint = 
        myListView.PointToClient(new Point(e.X, e.Y));

    // Retrieve the index of the item closest to the mouse pointer. 
    int targetIndex = myListView.InsertionMark.NearestIndex(targetPoint);

    // Confirm that the mouse pointer is not over the dragged item. 
    if (targetIndex > -1) 
        // Determine whether the mouse pointer is to the left or 
        // the right of the midpoint of the closest item and set 
        // the InsertionMark.AppearsAfterItem property accordingly.
        Rectangle itemBounds = myListView.GetItemRect(targetIndex);
        if ( targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) )
            myListView.InsertionMark.AppearsAfterItem = true;
            myListView.InsertionMark.AppearsAfterItem = false;

    // Set the location of the insertion mark. If the mouse is 
    // over the dragged item, the targetIndex value is -1 and 
    // the insertion mark disappears.
    myListView.InsertionMark.Index = targetIndex;

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.

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft