Bagikan melalui


ListBox.ObjectCollection.RemoveAt(Int32) Metode

Definisi

Menghapus item pada indeks yang ditentukan dalam koleksi.

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)

Parameter

index
Int32

Indeks berbasis nol dari item yang akan dihapus.

Penerapan

Pengecualian

Parameter index kurang dari nol atau lebih besar dari atau sama dengan nilai Count properti ListBox.ObjectCollection kelas.

Contoh

Contoh kode berikut menunjukkan cara menggunakan SelectedIndex properti dengan TopIndex properti untuk memindahkan item yang saat ini dipilih ke bagian atas daftar item di area ListBoxtampilan . Contoh selanjutnya menunjukkan cara menghapus item menggunakan RemoveAt metode System.Windows.Forms.ListBox.ObjectCollection kelas, dan cara menghapus semua pilihan item menggunakan ClearSelected metode . Kode pertama-tama memindahkan item yang saat ini dipilih di ListBox bagian atas daftar. Kode kemudian menghapus semua item sebelum item yang saat ini dipilih dan menghapus semua pilihan di ListBox. Contoh ini mengharuskan item yang ListBox berisi ditambahkan ke formulir dan item saat ini dipilih di 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

Keterangan

Saat Anda menghapus item dari daftar, indeks berubah untuk item berikutnya dalam daftar. Semua informasi tentang item yang dihapus dihapus. Anda dapat menggunakan metode ini untuk menghapus item tertentu dari daftar dengan menentukan indeks item yang akan dihapus dari daftar. Untuk menentukan item yang akan dihapus alih-alih indeks ke item, gunakan Remove metode . Untuk menghapus semua item dari daftar, gunakan Clear metode .

Berlaku untuk

Lihat juga