Aracılığıyla paylaş


ListBox.ObjectCollection.RemoveAt(Int32) Yöntem

Tanım

Koleksiyon içinde belirtilen dizindeki öğeyi kaldırır.

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)

Parametreler

index
Int32

Kaldırılacak öğenin sıfır tabanlı dizini.

Uygulamalar

Özel durumlar

index parametresi sıfırdan küçük veya sınıfın özelliğinin CountListBox.ObjectCollection değerine eşit veya sıfırdan büyük.

Örnekler

Aşağıdaki kod örneği, özelliğiyle özelliğini TopIndex kullanarak SelectedIndex seçili durumdaki öğeyi öğesinin görüntüleme alanındaki öğe listesinin en üstüne taşıma işlemini ListBoxgösterir. Örnekte, sınıfın yöntemini kullanarak öğelerin nasıl kaldırılacağı ve yöntemini System.Windows.Forms.ListBox.ObjectCollection kullanarak RemoveAt tüm öğe seçiminin nasıl temizılacağı gösterilmektedirClearSelected. Kod önce seçili durumdaki öğeyi ListBox listenin en üstüne taşır. Kod, seçili durumdaki öğeden önceki tüm öğeleri kaldırır ve içindeki ListBoxtüm seçimleri temizler. Bu örnek, içeren öğelerin forma eklenmesini ve içinde bir öğenin seçili ListBoxolmasını gerektirirListBox.

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

Açıklamalar

Listeden bir öğeyi kaldırdığınızda, listedeki sonraki öğeler için dizinler değişir. Kaldırılan öğe hakkındaki tüm bilgiler silinir. Listeden kaldırılacak öğenin dizinini belirterek belirli bir öğeyi listeden kaldırmak için bu yöntemi kullanabilirsiniz. Öğenin dizini yerine kaldırılacak öğeyi belirtmek için yöntemini kullanın Remove . Listedeki tüm öğeleri kaldırmak için yöntemini kullanın Clear .

Şunlara uygulanır

Ayrıca bkz.