Aracılığıyla paylaş


ListBox.SelectedIndex Özellik

Tanım

içinde ListBoxseçili durumdaki öğenin sıfır tabanlı dizinini alır veya ayarlar.

public:
 virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public override int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer

Özellik Değeri

Seçili durumdaki öğenin sıfır tabanlı dizini. Hiçbir öğe seçilmediyse negatif değer (-1) döndürülür.

Öznitelikler

Özel durumlar

Atanan değer -1'den küçük veya öğe sayısına eşit veya daha büyük.

SelectionMode özelliği None olarak ayarlanmıştır.

Ö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

Standart ListBoxiçin, içinde seçilen ListBoxöğenin dizinini belirlemek için bu özelliği kullanabilirsiniz. öğesinin SelectionModeListBox özelliği ya da SelectionMode.MultiSimpleSelectionMode.MultiExtended (çoklu seçim ListBoxolduğunu gösterir) olarak ayarlanırsa ve listede birden çok öğe seçilirse, bu özellik dizini seçilen herhangi bir öğeye döndürebilir.

Çoklu seçimdeki ListBoxtüm seçili öğelerin dizinlerini içeren bir koleksiyonu almak için özelliğini kullanın SelectedIndices . içinde ListBoxseçili olan öğeyi almak istiyorsanız özelliğini kullanın SelectedItem . Ayrıca, çok seçimli SelectedItemsListBoxbir içindeki tüm seçili öğeleri almak için özelliğini kullanabilirsiniz.

Şunlara uygulanır

Ayrıca bkz.