ListBox.SelectedItem 属性

定义

获取或设置 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 属性读取项的文本,并使用第一个 ListBoxSelectedItem 返回的文本对不同 ListBox 调用 FindString 方法。The 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. 此示例要求将两个名为 listBox1listBox2ListBox 控件添加到窗体中,并且两个 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. 该示例还要求在示例中定义的事件处理方法连接到 listBox1SelectedIndexChanged 事件。The 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,你可以使用此属性来确定在 ListBox中选择了哪一项。For a standard ListBox, you can use this property to determine which item is selected in the ListBox. 如果 ListBoxSelectionMode 属性设置为 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 属性获取多选 ListBox中的所有选择的索引。In addition, you can use the SelectedIndices property to obtain all the selected indexes in a multiple-selection ListBox.

适用于