Index Property
Collapse the table of content
Expand the table of content

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;

.NET Framework
Available since 2.0
Return to top
© 2016 Microsoft