ListBox.SelectedItem ListBox.SelectedItem ListBox.SelectedItem ListBox.SelectedItem Property

定义

获取或设置 ListBox 中的当前选定项。Gets or sets the currently selected item in the ListBox.

public:
 property System::Object ^ SelectedItem { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object SelectedItem { get; set; }
member this.SelectedItem : obj with get, set
Public Property SelectedItem As Object

属性值

表示控件中当前选定内容的对象。An object that represents the current selection in the control.

示例

下面的代码示例演示如何使用SelectedIndexChanged事件来搜索和选择中不同的项ListBox控件。The following code example demonstrates how to use the SelectedIndexChanged event to search for and select an item in a different ListBox control. 该示例使用SelectedIndexChanged事件来确定何时中的选定的项ListBox发生更改。The example uses the SelectedIndexChanged event to determine when the selected item in the ListBox is changed. 示例代码会读取项使用的文本SelectedItem属性并调用FindString方法在不同ListBox使用返回的文本SelectedItem在第一个ListBoxThe example code then reads the text of the item using the SelectedItem property and calls the FindString method on a different ListBox using the text returned by SelectedItem in the first ListBox. 如果在其他中找到的项ListBox,选择项。If an item is found in the other ListBox, the item is selected. 此示例需要两个ListBox控件,分别命名为listBox1listBox2,已添加到窗体,同时ListBox控件包含相同的项。This example requires that two ListBox controls, named listBox1 and listBox2, have been added to a form and that both ListBox controls contain items that are identical. 该示例还需要在示例中定义的事件处理方法连接到SelectedIndexChanged事件的listBox1The example also requires that the event-handling method defined in the example is connected to the SelectedIndexChanged event of listBox1.

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 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

注解

为标准ListBox,可以使用此属性以确定哪一项选择中ListBoxFor a standard ListBox, you can use this property to determine which item 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 any selected item.

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

适用于