ListBox.SelectedIndex ListBox.SelectedIndex ListBox.SelectedIndex ListBox.SelectedIndex Property

定义

获取或设置 ListBox 中当前选定项的从零开始的索引。Gets or sets the zero-based index of the currently selected item in a 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; }
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer

属性值

当前选定项的从零开始的索引。A zero-based index of the currently selected item. 如果未选定任何项,则返回值为负一 (-1)。A value of negative one (-1) is returned if no item is selected.

异常

指定的值小于 -1,或者大于或等于项的计数。The assigned value is less than -1 or greater than or equal to the item count.

示例

下面的代码示例演示如何使用SelectedIndex具有属性TopIndex属性以将当前所选的项移动到的显示区域中的项列表的顶部ListBoxThe following code example demonstrates how to use the SelectedIndex property with the TopIndex property to move the currently selected item to the top of the list of items in the display area of the ListBox. 该示例还演示如何删除使用的项RemoveAt方法System.Windows.Forms.ListBox.ObjectCollection类,以及如何清除所有项选择使用ClearSelected方法。The example further demonstrates how to remove items using the RemoveAt method of the System.Windows.Forms.ListBox.ObjectCollection class, and how to clear all item selection using the ClearSelected method. 该代码首先移动当前选定的项ListBox到列表的顶部。The code first moves the currently selected item in the ListBox to the top of the list. 然后,代码中移除当前所选的项之前的所有项并清除所有选择中的ListBoxThe code then removes all items before the currently selected item and clears all selections in the ListBox. 此示例需要ListBox包含项添加到窗体和项是当前选择中ListBoxThis example requires that a ListBox containing items is added to a form and that an item is currently selected in the 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 'RemoveTopItems

注解

为标准ListBox,可以使用此属性以确定中选定项的索引ListBoxFor a standard ListBox, you can use this property to determine the index of the item that is selected in the ListBox. 如果SelectionMode的属性ListBox设置为SelectionMode.MultiSimpleSelectionMode.MultiExtended(指示多选ListBox) 和在列表中选择多个项,此属性可以返回任何选定的项的索引。If the SelectionMode property of the ListBox is set to either SelectionMode.MultiSimple or SelectionMode.MultiExtended (which indicates a multiple-selection ListBox) and multiple items are selected in the list, this property can return the index to any selected item.

若要检索包含的所有索引集合中的选定项多选ListBox,使用SelectedIndices属性。To retrieve a collection containing the indexes of all selected items in a multiple-selection ListBox, use the SelectedIndices property. 如果你想要获取中当前选定的项ListBox,使用SelectedItem属性。If you want to obtain the item that is currently selected in the ListBox, use the SelectedItem property. 此外,还可以使用SelectedItems属性可以获取多个选定内容中的所有选定的项ListBoxIn addition, you can use the SelectedItems property to obtain all the selected items in a multiple-selection ListBox.

适用于

另请参阅