ListBox.ObjectCollection.RemoveAt(Int32) ListBox.ObjectCollection.RemoveAt(Int32) ListBox.ObjectCollection.RemoveAt(Int32) ListBox.ObjectCollection.RemoveAt(Int32) Method

定義

移除這個集合中位於指定索引處的項目。Removes the item at the specified index within the collection.

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)

參數

index
Int32 Int32 Int32 Int32

要移除之項目的以零為起始的索引。The zero-based index of the item to remove.

實作

例外狀況

index 參數小於零,或者大於等於 Count 類別的 ListBox.ObjectCollection 屬性值。The index parameter is less than zero or greater than or equal to the value of the Count property of the ListBox.ObjectCollection class.

範例

下列程式碼範例示範如何使用SelectedIndex具有屬性TopIndex屬性,以將目前選取的項目移至的顯示區域中的項目清單的頂端ListBoxThe following code example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. 進一步的範例將示範如何移除使用的項目RemoveAt方法System.Windows.Forms.ListBox.ObjectCollection類別,以及如何清除所有項目選取項目使用ClearSelected方法。The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. 程式碼第一次將目前選取的項目ListBox到清單頂端。The code first moves the currently selected item in the ListBox to the top of the list. 程式碼然後移除目前選取的項目之前的所有項目,並清除所有的選取項目中ListBoxThe code then removes all items before the currently selected item and clears all selections in the ListBox. 這個範例需要ListBox包含項目已加入至表單和中的項目是目前選取ListBoxThis example requires that a ListBox containing items is added to a form and that an item is currently selected in the 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 'RemoveTopItems

備註

當您從清單移除項目時,在清單中的後續項目索引將會變更。When you remove an item from the list, the indexes change for subsequent items in the list. 已移除的項目相關的所有資訊都會都刪除。All information about the removed item is deleted. 您可以使用這個方法,從清單中移除特定項目,藉由指定要從清單中移除之項目的索引。You can use this method to remove a specific item from the list by specifying the index of the item to remove from the list. 若要指定要移除而不是項目的索引的項目,請使用Remove方法。To specify the item to remove instead of the index to the item, use the Remove method. 若要從清單中移除所有項目,使用Clear方法。To remove all items from the list, use the Clear method.

適用於

另請參閱