ListViewInsertionMark.Index Egenskap

Definition

Hämtar eller anger indexet för objektet bredvid vilket insättningsmarkeringen visas.

public:
 property int Index { int get(); void set(int value); };
public int Index { get; set; }
member this.Index : int with get, set
Public Property Index As Integer

Egenskapsvärde

Indexet för objektet bredvid vilket insättningsmarkeringen visas eller -1 när insättningsmarkeringen är dold.

Exempel

I följande kodexempel visas hur du använder ListView infogningsmarkeringsfunktionen och implementerar omordning av dra och släpp-objekt med hjälp av standarddraghändelserna. Infogningsmärkets position uppdateras i en hanterare för Control.DragOver händelsen. I den här hanteraren jämförs muspekarens position med mittpunkten för det närmaste objektet och resultatet används för att avgöra om insättningsmarkeringen visas till vänster eller höger om objektet.

Det fullständiga exemplet finns i översiktsreferensavsnittet ListViewInsertionMark .

// Moves the insertion mark as the item is dragged.
void myListView_DragOver( Object^ /*sender*/, DragEventArgs^ e )
{
   // Retrieve the client coordinates of the mouse pointer.
   Point targetPoint = myListView->PointToClient( 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;
}
// 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;
}
' Moves the insertion mark as the item is dragged.
Private Sub myListView_DragOver(sender As Object, e As DragEventArgs)
    ' Retrieve the client coordinates of the mouse pointer.
    Dim targetPoint As Point = myListView.PointToClient(New Point(e.X, e.Y))
    
    ' Retrieve the index of the item closest to the mouse pointer.
    Dim targetIndex As Integer = _
        myListView.InsertionMark.NearestIndex(targetPoint)
    
    ' Confirm that the mouse pointer is not over the dragged item.
    If targetIndex > -1 Then
        ' 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.
        Dim itemBounds As Rectangle = myListView.GetItemRect(targetIndex)
        If targetPoint.X > itemBounds.Left + (itemBounds.Width / 2) Then
            myListView.InsertionMark.AppearsAfterItem = True
        Else
            myListView.InsertionMark.AppearsAfterItem = False
        End If
    End If
    
    ' 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
End Sub

Kommentarer

Insättningsmarkeringen visas till vänster om objektet med det angivna indexet AppearsAfterItem om egenskapen är inställd på false. Annars visas den till höger om objektet. Insättningsmarkeringen försvinner automatiskt när ett släpat objekt infogas i ListView.Items samlingen. Om du vill ta bort insättningsmarkeringen manuellt, till exempel när ett objekt dras till sig själv eller dras utanför kontrollen, anger du Index värdet till -1.

NearestIndex Använd metoden för att hämta indexet för objektet närmast en angiven plats, till exempel platsen för muspekaren när du placerar ett objekt i en dra och släpp-åtgärd.

Gäller för

Se även