ListBox.ObjectCollection.RemoveAt(Int32) Metoda

Definicja

Usuwa element w określonym indeksie w kolekcji.

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)

Parametry

index
Int32

Liczony od zera indeks elementu, który ma zostać usunięty.

Implementuje

Wyjątki

Parametr index jest mniejszy niż zero lub większy lub równy wartości Count właściwości ListBox.ObjectCollection klasy.

Przykłady

Poniższy przykład kodu pokazuje, jak użyć SelectedIndex właściwości z właściwością TopIndex , aby przenieść aktualnie wybrany element na górę listy elementów w obszarze ListBoxwyświetlania obiektu . W przykładzie pokazano również, jak usunąć elementy przy użyciu RemoveAt metody System.Windows.Forms.ListBox.ObjectCollection klasy i jak wyczyścić wszystkie zaznaczenie elementu przy użyciu ClearSelected metody . Kod najpierw przenosi aktualnie wybrany element w ListBox górnej części listy. Następnie kod usuwa wszystkie elementy przed aktualnie wybranym elementem i czyści wszystkie opcje w elemencie ListBox. Ten przykład wymaga dodania ListBox elementów zawierających do formularza, a element jest obecnie zaznaczony w elemencie 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

Uwagi

Po usunięciu elementu z listy indeksy zmieniają się dla kolejnych elementów na liście. Wszystkie informacje o usuniętym elemencie są usuwane. Tej metody można użyć do usunięcia określonego elementu z listy, określając indeks elementu do usunięcia z listy. Aby określić element do usunięcia zamiast indeksu do elementu, użyj Remove metody . Aby usunąć wszystkie elementy z listy, użyj Clear metody .

Dotyczy

Zobacz też