Sdílet prostřednictvím


ListBox.ObjectCollection.RemoveAt(Int32) Metoda

Definice

Odebere položku v zadaném indexu v kolekci.

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

Index položky založený na nule, který chcete odebrat.

Implementuje

Výjimky

Parametr index je menší než nula nebo větší nebo roven hodnotě Count vlastnosti ListBox.ObjectCollection třídy.

Příklady

Následující příklad kódu ukazuje, jak použít SelectedIndex vlastnost s TopIndex vlastností přesunout aktuálně vybranou položku na začátek seznamu položek v oblasti zobrazení ListBox. Příklad dále ukazuje, jak odebrat položky pomocí RemoveAt metody System.Windows.Forms.ListBox.ObjectCollection třídy a jak vymazat výběr všech položek pomocí ClearSelected metody. Kód nejprve přesune aktuálně vybranou položku v ListBox horní části seznamu. Kód pak odebere všechny položky před aktuálně vybranou položkou a vymaže všechny výběry v souboru ListBox. Tento příklad vyžaduje přidání ListBox obsahujících položek do formuláře a že položka je aktuálně vybrána v objektu 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

Poznámky

Když odeberete položku ze seznamu, indexy se změní pro další položky v seznamu. Odstraní se všechny informace o odebrané položce. Tuto metodu můžete použít k odebrání konkrétní položky ze seznamu zadáním indexu položky, kterou chcete ze seznamu odebrat. Chcete-li zadat položku, která se má odebrat místo indexu položky, použijte metodu Remove . Pokud chcete ze seznamu odebrat všechny položky, použijte metodu Clear .

Platí pro

Viz také