Export (0) Print
Expand All

ListViewInsertionMark.AppearsAfterItem Property

Gets or sets a value indicating whether the insertion mark appears to the right of the item with the index specified by the Index property.

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

public bool AppearsAfterItem { get; set; }

Property Value

Type: System.Boolean
true if the insertion mark appears to the right of the item with the index specified by the Index property; otherwise, false. The default is false.

The NearestIndex method lets you find the item closest to the mouse pointer, but you must do your own calculations to determine whether the insertion mark should appear before or after this item.

To calculate the value needed for the AppearsAfterItem property, follow these steps:

  1. Use the NearestIndex method to retrieve the index of the item closest to the mouse pointer.

  2. Pass the index value to the ListView.GetItemRect method to retrieve the bounding rectangle of the item.

  3. If the mouse pointer is located to the left of the midpoint of the bounding rectangle, set the AppearsAfterItem property to false; otherwise, set it to true.

For more information, see the ListViewInsertionMark overview reference topic.

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;
        }
        else
        {
            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

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft