Freigeben über


ListBox.SelectedIndex-Eigenschaft

Ruft den nullbasierten Index des derzeit ausgewählten Elements in einer ListBox ab oder legt diesen fest.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
<BindableAttribute(True)> _
Public Overrides Property SelectedIndex As Integer
'Usage
Dim instance As ListBox
Dim value As Integer

value = instance.SelectedIndex

instance.SelectedIndex = value
[BindableAttribute(true)] 
public override int SelectedIndex { get; set; }
[BindableAttribute(true)] 
public:
virtual property int SelectedIndex {
    int get () override;
    void set (int value) override;
}
/** @property */
public int get_SelectedIndex ()

/** @property */
public void set_SelectedIndex (int value)
public override function get SelectedIndex () : int

public override function set SelectedIndex (value : int)

Eigenschaftenwert

Ein nullbasierter Index des derzeit ausgewählten Elements. Wenn kein Element ausgewählt ist, wird ein Wert von -1 zurückgegeben.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentOutOfRangeException

Der zugeordnete Wert ist kleiner als -1 oder größer oder gleich der Elementanzahl.

ArgumentException

Die SelectionMode-Eigenschaft ist auf None festgelegt.

Hinweise

Mithilfe dieser Eigenschaft können Sie für eine Standard-ListBox den Index des in der ListBox ausgewählten Elements bestimmen. Wenn die SelectionMode-Eigenschaft der ListBox auf SelectionMode.MultiSimple oder SelectionMode.MultiExtended festgelegt ist (wodurch eine ListBox mit Mehrfachauswahl angegeben wird) und mehrere Elemente in der Liste ausgewählt sind, kann diese Eigenschaft den Index für jedes ausgewählte Element zurückgeben.

Wenn Sie eine Auflistung abrufen möchten, die die Indizes sämtlicher ausgewählter Elemente in einer ListBox mit Mehrfachauswahl enthält, verwenden Sie die SelectedIndices-Eigenschaft. Wenn Sie das derzeit in ListBox ausgewählte Element abrufen möchten, verwenden Sie die SelectedItem-Eigenschaft. Zusätzlich können Sie über die SelectedItems-Eigenschaft sämtliche ausgewählten Elemente in einer ListBox mit Mehrfachauswahl abrufen.

Beispiel

Im folgenden Codebeispiel wird veranschaulicht, wie mit der SelectedIndex-Eigenschaft und der TopIndex-Eigenschaft das aktuell ausgewählte Element an die obere Position der Elementliste im Anzeigebereich von ListBox verschoben wird. Darüber hinaus wird veranschaulicht, wie Elemente mithilfe der RemoveAt-Methode der System.Windows.Forms.ListBox.ObjectCollection-Klasse entfernt werden und wie die gesamte Elementauswahl mithilfe der ClearSelected-Methode aufgehoben wird. Im Code wird zunächst das derzeit ausgewählte Element in der ListBox an die erste Stelle in der Liste verschoben. Anschließend werden alle Elemente vor dem derzeit ausgewählten Element entfernt, und die gesamte Auswahl in der ListBox wird aufgehoben. Für dieses Beispiel muss einem Formular eine ListBox mit Elementen hinzugefügt werden, und in der ListBox muss gegenwärtig ein Element ausgewählt sein.

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 'RemoveTopItems
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 void RemoveTopItems()
{
    // Determine if the currently selected item in the ListBox 
    // is the item displayed at the top in the ListBox.
    if (listBox1.get_TopIndex() != listBox1.get_SelectedIndex()) {
        // Make the currently selected item the top item in the ListBox.
        listBox1.set_TopIndex(listBox1.get_SelectedIndex());
    }
    // Remove all items before the top item in the ListBox.
    for (int x = listBox1.get_SelectedIndex() - 1; x >= 0; x--) {
        listBox1.get_Items().RemoveAt(x);
    }
    // Clear all selections in the ListBox.
    listBox1.ClearSelected();
} //RemoveTopItems

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

ListBox-Klasse
ListBox-Member
System.Windows.Forms-Namespace
SelectedIndices
SelectedItems
SelectedItem