ListBox.SelectedIndex 属性

获取或设置 ListBox 中当前选定项的从零开始的索引。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<BindableAttribute(True)> _
Public Overrides Property SelectedIndex As Integer
用法
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)

属性值

当前选定项的从零开始的索引。如果未选定任何项,则返回值为负一 (-1)。

异常

异常类型 条件

ArgumentOutOfRangeException

指定的值小于 -1,或者大于或等于项的计数。

ArgumentException

SelectionMode 属性设置为 None

备注

对于标准 ListBox,可使用此属性确定 ListBox 中选定的项的索引。如果 ListBoxSelectionMode 属性设置为 SelectionMode.MultiSimpleSelectionMode.MultiExtended(它指示多重选择 ListBox),并在该列表中选定多个项,则此属性可返回任何选定项的索引。

若要检索包含多重选择 ListBox 中所有选定项的索引的集合,请使用 SelectedIndices 属性。如果要获得 ListBox 中的当前选定项,请使用 SelectedItem 属性。另外,还可使用 SelectedItems 属性获取多重选择 ListBox 中的所有选定项。

示例

下面的代码示例演示如何使用 SelectedIndex 属性及 TopIndex 属性来将当前选定的项移到 ListBox 显示区域中的项列表的顶部。该示例还演示了如何使用 System.Windows.Forms.ListBox.ObjectCollection 类的 RemoveAt 方法移除项以及如何使用 ClearSelected 方法清除所有选择的项。这段代码首先将 ListBox 中当前选定的项移到列表的顶部。然后移除当前选定的项之前的所有项并清除 ListBox 中的所有选择。此示例要求已将包含项的 ListBox 添加到窗体,而且当前已在 ListBox 中选择了一个项。

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

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

ListBox 类
ListBox 成员
System.Windows.Forms 命名空间
SelectedIndices
SelectedItems
SelectedItem