ListBox.ObjectCollection.RemoveAt(Int32) Método

Definição

Remove o item no índice especificado dentro da coleção.

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)

Parâmetros

index
Int32

O índice em base zero do item a remover.

Implementações

Exceções

O index parâmetro é menor que zero ou maior ou igual ao valor da Count propriedade da ListBox.ObjectCollection classe.

Exemplos

O exemplo de código seguinte demonstra como usar a SelectedIndex propriedade com a TopIndex propriedade para mover o item atualmente selecionado para o topo da lista de itens na área de exibição do ListBox. O exemplo demonstra ainda como remover itens usando o método RemoveAt da classe System.Windows.Forms.ListBox.ObjectCollection, e como limpar toda a seleção de itens usando o método ClearSelected. O código primeiro move o item ListBox atualmente selecionado para o topo da lista. O código remove então todos os itens antes do item atualmente selecionado e limpa todas as seleções no ListBoxarquivo . Este exemplo exige que um ListBox item contendo seja adicionado a um formulário e que um item esteja atualmente selecionado no ListBoxformulário .

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

Observações

Quando remove um item da lista, os índices mudam para os itens seguintes da lista. Toda a informação sobre o item removido é apagada. Pode usar este método para remover um item específico da lista, especificando o índice do item a remover da lista. Para especificar o item a remover em vez do índice do item, use o Remove método. Para remover todos os itens da lista, use o Clear método.

Aplica-se a

Ver também