ListBox.TopIndex 속성

정의

ListBox에서 가장 먼저 표시되는 항목의 인덱스를 가져오거나 설정합니다.

public:
 property int TopIndex { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int TopIndex { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.TopIndex : int with get, set
Public Property TopIndex As Integer

속성 값

Int32

컨트롤에서 가장 먼저 표시되는 항목의 0부터 시작하는 인덱스입니다.

특성

예제

다음 코드 예제에서는 속성과 함께 TopIndex 속성을 사용하여 SelectedIndex 현재 선택한 항목을 표시 영역ListBox의 항목 목록 맨 위로 이동하는 방법을 보여 줍니다. 이 예제에서는 클래스의 System.Windows.Forms.ListBox.ObjectCollection 메서드를 사용하여 항목을 제거하는 방법과 메서드를 사용하여 RemoveAt 모든 항목 선택을 지우는 ClearSelected 방법을 보여 줍니다. 코드는 먼저 현재 선택한 항목을 ListBox 목록의 맨 위로 이동합니다. 그런 다음, 코드는 현재 선택한 항목 앞에 있는 모든 항목을 ListBox제거하고 . 이 예제에서는 포함하는 항목이 ListBox 폼에 추가되고 항목이 현재 선택되어 있어야 ListBox합니다.

private:
   void RemoveTopItems()
   {
      // Determine if the currently selected item in the ListBox 
      // is the item displayed at the top in the ListBox.
      if ( listBox1->TopIndex != listBox1->SelectedIndex )

      // Make the currently selected item the top item in the ListBox.
      listBox1->TopIndex = listBox1->SelectedIndex;

      // Remove all items before the top item in the ListBox.
      for ( int x = (listBox1->SelectedIndex - 1); x >= 0; x-- )
      {
         listBox1->Items->RemoveAt( x );
      }

      // Clear all selections in the ListBox.
      listBox1->ClearSelected();
   }
private void RemoveTopItems()
{
   // Determine if the currently selected item in the ListBox 
   // is the item displayed at the top in the ListBox.
   if (listBox1.TopIndex != listBox1.SelectedIndex)
      // Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex;

   // Remove all items before the top item in the ListBox.
   for (int x = (listBox1.SelectedIndex -1); x >= 0; x--)
   {
      listBox1.Items.RemoveAt(x);
   }

   // Clear all selections in the ListBox.
   listBox1.ClearSelected();
}
Private Sub RemoveTopItems()
   ' Determine if the currently selected item in the ListBox 
   ' is the item displayed at the top in the ListBox.
   If listBox1.TopIndex <> listBox1.SelectedIndex Then
      ' Make the currently selected item the top item in the ListBox.
      listBox1.TopIndex = listBox1.SelectedIndex
   End If
   ' Remove all items before the top item in the ListBox.
   Dim x As Integer
   For x = listBox1.SelectedIndex - 1 To 0 Step -1
      listBox1.Items.RemoveAt(x)
   Next x

   ' Clear all selections in the ListBox.
   listBox1.ClearSelected()
End Sub

설명

처음에는 인덱스 위치가 0인 항목이 표시되는 영역 ListBox의 맨 위에 있습니다. 내용이 ListBox 스크롤된 경우 다른 항목이 컨트롤의 표시 영역 맨 위에 있을 수 있습니다. 이 속성을 사용 하 여 현재 컨트롤의 표시 영역 맨 위에 위치 하는 항목의 인덱스 내에서 ListBox.ObjectCollection ListBox 가져올 수 있습니다. 이 속성을 사용하여 컨트롤의 표시되는 영역 맨 위에 있는 목록의 항목을 배치할 수도 있습니다.

적용 대상