ListBox.ObjectCollection.RemoveAt(Int32) Método

Definición

Quita el elemento en el índice especificado de la colección.

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

Índice de base cero del elemento que se va a quitar.

Implementaciones

Excepciones

El parámetro index es menor que cero o igual o mayor que el valor de la propiedad Count de la clase ListBox.ObjectCollection.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la SelectedIndex propiedad con la TopIndex propiedad para mover el elemento seleccionado actualmente a la parte superior de la lista de elementos del área de presentación de ListBox. En el ejemplo se muestra aún más cómo quitar elementos mediante el RemoveAt método de la System.Windows.Forms.ListBox.ObjectCollection clase y cómo borrar toda la selección de elementos mediante el ClearSelected método . El código mueve primero el elemento seleccionado actualmente en ListBox la parte superior de la lista. A continuación, el código quita todos los elementos antes del elemento seleccionado actualmente y borra todas las selecciones de ListBox. En este ejemplo se requiere que se agregue un ListBox elemento contenedor a un formulario y que un elemento esté seleccionado actualmente en .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

Comentarios

Al quitar un elemento de la lista, los índices cambian para los elementos posteriores de la lista. Se elimina toda la información sobre el elemento quitado. Puede usar este método para quitar un elemento específico de la lista especificando el índice del elemento que se va a quitar de la lista. Para especificar el elemento que se va a quitar en lugar del índice al elemento, use el Remove método . Para quitar todos los elementos de la lista, use el Clear método .

Se aplica a

Consulte también