共用方式為


ListBox.ObjectCollection.RemoveAt(Int32) 方法

定義

移除集合中指定索引的項目。

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

要移除的項目的零基指數。

實作

例外狀況

參數index小於零或大於或等於類別ListBox.ObjectCollection性質的值Count

範例

以下程式碼範例示範如何利用 SelectedIndex 該屬性, TopIndex 將目前選取的項目移動到顯示區域 ListBox中項目列表的最上方。 範例進一步示範如何使用類別的方法System.Windows.Forms.ListBox.ObjectCollection移除物品RemoveAt,以及如何用該ClearSelected方法清除所有物品選擇。 程式碼會先將目前選取的 ListBox 項目移到清單頂端。 程式碼會移除目前選取項目前的所有項目,並清除 ListBox. 此範例要求 ListBox 將包含項目加入表單,且目前在 中選取 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

備註

當你從清單中移除某個項目時,列表中後續項目的索引也會改變。 所有關於被移除物品的資訊都會被刪除。 你可以用這個方法,透過指定要移除的項目的索引,來從清單中移除特定項目。 若要指定要移除的項目,而不是該項目的索引,請使用該 Remove 方法。 要從清單中移除所有項目,請使用這個 Clear 方法。

適用於

另請參閱