ListViewInsertionMark.NearestIndex(Point) Méthode

Définition

Récupère l'index de l'élément le plus proche du point spécifié.

public:
 int NearestIndex(System::Drawing::Point pt);
public int NearestIndex (System.Drawing.Point pt);
member this.NearestIndex : System.Drawing.Point -> int
Public Function NearestIndex (pt As Point) As Integer

Paramètres

pt
Point

Point représentant l'emplacement à partir duquel rechercher l'élément le plus proche.

Retours

L'index de l'élément le plus proche du point spécifié ou -1 si l'élément le plus proche est l'élément actuellement glissé.

Exemples

L’exemple de code suivant montre comment utiliser la ListView fonctionnalité de marque d’insertion et implémente la réorganisation des éléments par glisser-déplacer à l’aide des événements de glisser-déplacer standard. La position de la marque d’insertion est mise à jour dans un gestionnaire pour l’événement Control.DragOver . Dans ce gestionnaire, la position du pointeur de la souris est comparée au point central de l’élément le plus proche, et le résultat est utilisé pour déterminer si la marque d’insertion apparaît à gauche ou à droite de l’élément.

Pour obtenir l’exemple complet, consultez la rubrique de référence de vue d’ensemble 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

Remarques

Cette méthode vous permet de localiser l’élément le plus proche du pointeur de la souris lors d’une opération de glisser-déplacer. Utilisez la valeur d’index retournée pour définir la Index propriété. Lorsque l’élément le plus proche du pointeur de la souris est l’élément en cours de déplacement, la valeur de retour de cette méthode est -1. Dans ce cas, le fait de définir la Index propriété sur cette valeur masque la marque d’insertion.

Cette méthode recherche l’élément le plus proche, quel que soit l’emplacement du pointeur de la souris, tandis que la ListView.GetItemAt méthode retourne l’élément à l’emplacement spécifié uniquement, ou null s’il n’y a pas d’élément à cet emplacement. La ListView.GetItemAt méthode retourne null, par exemple, lorsque le pointeur de la souris se trouve entre deux éléments. Pour cette raison, vous devez toujours utiliser la méthode lors de l’utilisation NearestIndex d’une opération de glisser-déplacer pour positionner des éléments.

S’applique à

Voir aussi