ListViewInsertionMark.NearestIndex Method (Point)


Retrieves the index of the item closest to the specified point.

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

public int NearestIndex(
	Point pt


Type: System.Drawing.Point

A Point representing the location from which to find the nearest item.

Return Value

Type: System.Int32

The index of the item closest to the specified point or -1 if the closest item is the item currently being dragged.

This method lets you locate the item closest to the mouse pointer when doing a drag-and-drop operation. Use the index value returned to set the Index property. When the item closest to the mouse pointer is the item being dragged, the return value of this method is -1. In this case, setting the Index property to this value hides the insertion mark.

This method finds the closest item regardless of where the mouse pointer is located, while the ListView.GetItemAt method returns the item at the specified location only, or null if there is no item at that location. The ListView.GetItemAt method returns null, for example, when the mouse pointer is located between two items. For this reason, you should always use the NearestIndex method when using a drag-and-drop operation to position items.

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