Aracılığıyla paylaş


ListBox.SelectedIndex Özellik

Tanım

içinde seçili durumdaki öğenin ListBoxsı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

Int32

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

Öznitelikler

Özel durumlar

Atanan değer -1'den küçük veya öğe sayımından büyük veya buna eşit.

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 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 ListBox öğeyi listenin en üstüne taşır. Kod daha sonra seçili durumdaki öğeden önceki tüm öğeleri kaldırır ve içindeki ListBoxtüm seçimleri temizler. Bu örnek, bir forma içeren öğelerin eklenmesini ve içinde şu anda bir öğenin seçilmesini ListBoxgerektirirListBox.

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 ListBoxbir için, içinde seçilen ListBoxöğenin dizinini belirlemek için bu özelliği kullanabilirsiniz. özelliğinin SelectionMode ListBox özelliği ya da SelectionMode.MultiSimple SelectionMode.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, birden çok seçimli SelectedItems ListBoxbir içindeki tüm seçili öğeleri almak için özelliğini kullanabilirsiniz.

Şunlara uygulanır

Ayrıca bkz.