ListBox.SelectedItem 属性

获取或设置 ListBox 中的当前选定项。

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

语法

声明
<BindableAttribute(True)> _
Public Property SelectedItem As Object
用法
Dim instance As ListBox
Dim value As Object

value = instance.SelectedItem

instance.SelectedItem = value
[BindableAttribute(true)] 
public Object SelectedItem { get; set; }
[BindableAttribute(true)] 
public:
property Object^ SelectedItem {
    Object^ get ();
    void set (Object^ value);
}
/** @property */
public Object get_SelectedItem ()

/** @property */
public void set_SelectedItem (Object value)
public function get SelectedItem () : Object

public function set SelectedItem (value : Object)

属性值

表示控件中当前选定内容的对象。

备注

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

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

示例

下面的代码示例演示如何使用 SelectedIndexChanged 事件搜索并选择不同 ListBox 控件中的项。该示例使用 SelectedIndexChanged 事件来确定 ListBox 中的选定项的更改时间。然后,该代码示例使用 SelectedItem 属性读取该项的文本并且使用第一个 ListBox 中的 SelectedItem 返回的文本调用其他 ListBox 上的 FindString 方法。如果在其他 ListBox 中找到项,则选择该项。该示例要求已将名为 listBox1listBox2 的两个 ListBox 控件添加到窗体中,并且这两个 ListBox 控件包含相同的项。该示例还要求示例中定义的事件处理方法已连接到 listBox1SelectedIndexChanged 事件。

Private Sub listBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox1.SelectedIndexChanged
   ' Get the currently selected item in the ListBox.
   Dim curItem As String = listBox1.SelectedItem.ToString()

   ' Find the string in ListBox2.
   Dim index As Integer = listBox2.FindString(curItem)
   ' If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
   If index = -1 Then
      MessageBox.Show("Item is not available in ListBox2")
   Else
      listBox2.SetSelected(index, True)
   End If
End Sub
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   // Get the currently selected item in the ListBox.
   string curItem = listBox1.SelectedItem.ToString();

   // Find the string in ListBox2.
   int index = listBox2.FindString(curItem);
   // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
   if(index == -1)
      MessageBox.Show("Item is not available in ListBox2");
   else
      listBox2.SetSelected(index,true);
}
private:
   void listBox1_SelectedIndexChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      // Get the currently selected item in the ListBox.
      String^ curItem = listBox1->SelectedItem->ToString();

      // Find the string in ListBox2.
      int index = listBox2->FindString( curItem );

      // If the item was not found in ListBox 2 display a message box,
      //  otherwise select it in ListBox2.
      if ( index == -1 )
            MessageBox::Show( "Item is not available in ListBox2" );
      else
            listBox2->SetSelected( index, true );
   }
private void listBox1_SelectedIndexChanged(Object sender, 
    System.EventArgs e)
{
    // Get the currently selected item in the ListBox.
    String curItem = listBox1.get_SelectedItem().ToString();
    // Find the string in ListBox2.
    int index = listBox2.FindString(curItem);
    // If the item was not found in ListBox 2 display a message box, 
    // otherwise select it in ListBox2.
    if (index == -1) {
        MessageBox.Show("Item is not available in ListBox2");
    }
    else {
        listBox2.SetSelected(index, true);
    }
} //listBox1_SelectedIndexChanged 

平台

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 命名空间