ListViewInsertionMark.Index Własność
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Pobiera lub ustawia indeks elementu, obok którego pojawia się znacznik wstawiania.
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
Wartość nieruchomości
Indeks elementu, obok którego pojawia się znacznik wstawiania lub -1, gdy znacznik wstawiania jest ukryty.
Przykłady
W poniższym przykładzie kodu pokazano, jak używać ListView funkcji znacznika wstawiania i implementuje zmienianie kolejności elementów przeciągania i upuszczania przy użyciu standardowych zdarzeń przeciągania. Pozycja znacznika wstawiania jest aktualizowana w procedurze obsługi zdarzenia Control.DragOver . W tej procedurze obsługi pozycja wskaźnika myszy jest porównywana z punktem środkowym najbliższego elementu, a wynik służy do określenia, czy znacznik wstawiania pojawia się po lewej lub prawej stronie elementu.
Pełny przykład można znaleźć w temacie referencyjnym ListViewInsertionMark przeglądu.
// 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
Uwagi
Znacznik wstawiania pojawia się po lewej stronie elementu z określonym indeksem, jeśli AppearsAfterItem właściwość jest ustawiona na false; w przeciwnym razie pojawia się po prawej stronie elementu. Znacznik wstawiania zniknie automatycznie po wstawieniu przeciągniętego elementu do kolekcji ListView.Items . Aby ręcznie usunąć znacznik wstawiania, na przykład gdy element zostanie przeciągnięty na siebie lub przeciągnięty poza kontrolkę, ustaw Index wartość -1.
NearestIndex Użyj metody , aby pobrać indeks elementu znajdującego się najbliżej określonej lokalizacji, na przykład lokalizację wskaźnika myszy podczas pozycjonowania elementu w operacji przeciągania i upuszczania.