ListBox.ObjectCollection.RemoveAt(Int32) Metoda

Definice

Odebere položku v zadaném indexu v rámci kolekce.

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

Z nuly vycházející index položky k odebrání.

Implementuje

Výjimky

Parametr index je menší než nula nebo větší než nebo rovno 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 do horní části 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šechny položky výběru 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, aby ListBox se do formuláře přidalo obsahující položky a že je položka aktuálně vybrána v souboru 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. Všechny informace o odebrané položce se odstraní. 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 odebrat všechny položky ze seznamu, použijte metodu Clear .

Platí pro

Viz také