다음을 통해 공유


ListViewInsertionMark.Index 속성

정의

삽입 표시가 나타나는 옆에 있는 항목의 인덱스를 가져오거나 설정합니다.

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 옆에 있는 항목의 인덱스입니다.

예제

다음 코드 예제에서는 삽입 표시 기능을 사용 ListView 하는 방법을 보여 줍니다 및 표준 끌기 이벤트를 사용 하 여 끌어서 놓기 항목 다시 정렬을 구현 합니다. 삽입 표시의 위치는 이벤트에 대한 처리기에서 업데이트됩니다 Control.DragOver . 이 처리기에서 마우스 포인터의 위치는 가장 가까운 항목의 중간점과 비교되며, 결과는 삽입 표시가 항목의 왼쪽 또는 오른쪽에 표시되는지 여부를 결정하는 데 사용됩니다.

전체 예제는 개요 참조 항목을 참조 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

설명

속성이 설정된 false경우 AppearsAfterItem 지정된 인덱스가 있는 항목의 왼쪽에 삽입 표시가 나타나고, 그렇지 않으면 항목의 오른쪽에 나타납니다. 끌어온 항목이 컬렉션에 ListView.Items 삽입되면 삽입 표시가 자동으로 사라집니다. 삽입 표시를 수동으로 제거하려면(예: 항목이 자체적으로 끌거나 컨트롤 외부로 끌 때) 값을 -1로 설정합니다 Index .

NearestIndex 이 메서드를 사용하여 끌어서 놓기 작업에서 항목을 배치할 때 마우스 포인터의 위치와 같이 지정된 위치에 가장 가까운 항목의 인덱스 검색을 수행합니다.

적용 대상

추가 정보