ListBox.SelectedIndex Właściwość

Definicja

Pobiera lub ustawia indeks zerowy aktualnie wybranego elementu w elemencie ListBox.

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

Wartość właściwości

Int32

Indeks oparty na zerowym aktualnie wybranym elemencie. Wartość ujemna (-1) jest zwracana, jeśli nie wybrano żadnego elementu.

Atrybuty

Wyjątki

Przypisana wartość jest mniejsza niż -1 lub większa lub równa liczbie elementów.

Właściwość SelectionMode ma ustawioną wartość None.

Przykłady

Poniższy przykład kodu pokazuje, jak użyć SelectedIndex właściwości z właściwością TopIndex , aby przenieść aktualnie wybrany element na górę listy elementów w obszarze ListBoxwyświetlania obiektu . W przykładzie pokazano również, jak usunąć elementy przy użyciu RemoveAt metody System.Windows.Forms.ListBox.ObjectCollection klasy i jak wyczyścić wszystkie zaznaczenie elementu przy użyciu ClearSelected metody . Kod najpierw przenosi aktualnie wybrany element w ListBox górnej części listy. Następnie kod usuwa wszystkie elementy przed aktualnie wybranym elementem i czyści wszystkie opcje w elemencie ListBox. Ten przykład wymaga dodania ListBox elementów zawierających do formularza, a element jest obecnie zaznaczony w elemencie 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

Uwagi

W przypadku standardu ListBoxmożna użyć tej właściwości do określenia indeksu elementu wybranego w elemencie ListBox. SelectionMode Jeśli właściwość ListBox obiektu jest ustawiona na SelectionMode.MultiSimple lub SelectionMode.MultiExtended (co wskazuje na wielokrotny wybórListBox), a na liście wybrano wiele elementów, ta właściwość może zwrócić indeks do dowolnego wybranego elementu.

Aby pobrać kolekcję zawierającą indeksy wszystkich wybranych elementów w wielokrotnym zaznaczeniu ListBox, użyj SelectedIndices właściwości . Jeśli chcesz uzyskać element, który jest aktualnie wybrany w elemencie ListBox, użyj SelectedItem właściwości . Ponadto można użyć SelectedItems właściwości , aby uzyskać wszystkie wybrane elementy w wielokrotnym zaznaczeniu ListBox.

Dotyczy

Zobacz też