ListBox.ObjectCollection.RemoveAt(Int32) 메서드

정의

컬렉션 내의 지정된 인덱스에 있는 항목을 제거합니다.

public:
 virtual void RemoveAt(int index);
public void RemoveAt (int index);
abstract member RemoveAt : int -> unit
override this.RemoveAt : int -> unit
Public Sub RemoveAt (index As Integer)

매개 변수

index
Int32

제거할 항목의 인덱스(0부터 시작)입니다.

구현

예외

index 매개 변수가 0보다 작거나 Count 클래스의 ListBox.ObjectCollection 속성 값보다 크거나 같은 경우

예제

다음 코드 예제에서는 속성과 함께 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

설명

목록에서 항목을 제거하면 목록의 후속 항목에 대한 인덱스가 변경됩니다. 제거 된 항목에 대 한 모든 정보가 삭제 됩니다. 목록에서 제거할 항목의 인덱스 지정 하 여 목록에서 특정 항목을 제거 하려면이 메서드를 사용할 수 있습니다. 항목 인덱스 대신 제거 하려면이 항목을 지정 하려면 사용 된 Remove 메서드. 목록에서 모든 항목을 제거하려면 메서드를 Clear 사용합니다.

적용 대상

추가 정보