Share via


ListViewInsertionMark.NearestIndex(Point) Yöntem

Tanım

Belirtilen noktaya en yakın öğenin dizinini alır.

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

Parametreler

pt
Point

Point En yakın öğenin bulun olduğu konumu temsil eden.

Döndürülenler

Belirtilen noktaya en yakın öğenin dizini veya en yakın öğe sürüklenmekte olan öğeyse -1.

Örnekler

Aşağıdaki kod örneği, ekleme işareti özelliğinin ListView nasıl kullanılacağını gösterir ve standart sürükleme olaylarını kullanarak öğeyi sürükleyip bırakma yeniden sıralamasını uygular. Ekleme işaretinin konumu, olay işleyicisinde Control.DragOver güncelleştirilir. Bu işleyicide, fare işaretçisinin konumu en yakın öğenin orta noktasıyla karşılaştırılır ve ekleme işaretinin öğenin solunda mı yoksa sağında mı göründüğünü belirlemek için sonuç kullanılır.

Tam örnek için genel bakış başvuru konusuna ListViewInsertionMark bakın.

// 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

Açıklamalar

Bu yöntem, sürükle ve bırak işlemi yaparken fare işaretçisine en yakın öğeyi bulmanızı sağlar. Özelliğini ayarlamak Index için döndürülen dizin değerini kullanın. Fare işaretçisine en yakın öğe sürüklenen öğe olduğunda, bu yöntemin dönüş değeri -1 olur. Bu durumda, özelliğinin bu değere ayarlanması Index ekleme işaretini gizler.

Bu yöntem, fare işaretçisinin bulunduğu konumdan bağımsız olarak en yakın öğeyi bulurken ListView.GetItemAt , yöntem öğeyi yalnızca belirtilen konumda döndürür veya null bu konumda öğe yoksa. ListView.GetItemAt yöntemi, örneğin, fare işaretçisi iki öğe arasında bulunduğunda döndürürnull. Bu nedenle, öğeleri konumlandırmak için sürükle ve bırak işlemi kullanırken her zaman yöntemini kullanmalısınız NearestIndex .

Şunlara uygulanır

Ayrıca bkz.