ListViewInsertionMark.Index プロパティ

定義

挿入マークが表示される位置の横にある項目のインデックスを取得または設定します。Gets or sets the index of the item next to which the insertion mark appears.

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

プロパティ値

挿入マークが表示される位置の横にある項目のインデックス。挿入マークが非表示の時には、-1 が返されます。The index of the item next to which the insertion mark appears or -1 when the insertion mark is hidden.

次のコード例は、 ListView挿入マーク機能を使用し、標準のドラッグイベントを使用してドラッグアンドドロップ項目の並べ替えを実装する方法を示しています。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. 挿入マークの位置は、 Control.DragOverイベントのハンドラーで更新されます。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.

完全な例についてはListViewInsertionMark 、「概要リファレンス」トピックを参照してください。For the complete example, see the ListViewInsertionMark overview reference topic.

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

注釈

AppearsAfterItemプロパティがにfalse設定されている場合、指定したインデックスを持つ項目の左側に挿入マークが表示されます。それ以外の場合は、項目の右側に表示されます。The insertion mark appears to the left of the item with the specified index if the AppearsAfterItem property is set to false; otherwise, it appears to the right of the item. ドラッグした項目がListView.Itemsコレクションに挿入されると、挿入マークは自動的に消えます。The insertion mark disappears automatically when a dragged item is inserted into the ListView.Items collection. 挿入マークを手動で削除するには (項目をドラッグしてコントロールの外側にドラッグする場合など)、 Index値を-1 に設定します。To remove the insertion mark manually, for example, when an item is dragged onto itself or dragged outside the control, set the Index value to -1.

NearestIndexメソッドを使用して、指定した位置に最も近い項目のインデックスを取得します。たとえば、ドラッグアンドドロップ操作で項目を配置するときのマウスポインターの位置などです。Use the NearestIndex method to retrieve the index of the item closest to a specified location, such as the location of the mouse pointer when positioning an item in a drag-and-drop operation.

適用対象

こちらもご覧ください