ListBox.SelectedIndex 屬性

定義

取得或設定 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

屬性值

目前選取項目之以零為起始的索引。 如果未選取任何項目,將傳回負一 (-1)。

屬性

例外狀況

指派的值小於 -1 或大於等於項目計數。

SelectionMode 屬性會設定為 None

範例

下列程式碼範例示範如何使用 SelectedIndex 屬性搭配 TopIndex 屬性,將目前選取的專案移至 顯示區域中 ListBox 專案清單頂端。 此範例會進一步示範如何使用 類別的 System.Windows.Forms.ListBox.ObjectCollection 方法移除專案 RemoveAt ,以及如何使用 ClearSelected 方法清除所有專案選取專案。 程式碼會先將 中 ListBox 目前選取的專案移至清單頂端。 然後,程式碼會移除目前選取專案之前的所有專案,並清除 中的所有 ListBox 選取專案。 此範例會 ListBox 要求將包含專案的 加入表單中,而且目前已在 中 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

備註

針對標準 ListBox ,您可以使用這個屬性來判斷 中 ListBox 選取之專案的索引。 如果 的 SelectionModeListBox 屬性設定為 SelectionMode.MultiSimpleSelectionMode.MultiExtended (,表示在清單中選取多重選取 ListBox) 和多個專案,則此屬性可以將索引傳回至任何選取的專案。

若要擷取集合,其中包含多重選取 ListBox 範圍中所有選取專案的索引,請使用 SelectedIndices 屬性。 如果您想要取得 目前在 中 ListBox 選取的專案,請使用 SelectedItem 屬性。 此外,您可以使用 SelectedItems 屬性來取得多重選取 ListBox 專案中的所有選取專案。

適用於

另請參閱