ListBox.SelectedItem 屬性

定義

取得或設定 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; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public object? SelectedItem { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedItem : obj with get, set
Public Property SelectedItem As Object

屬性值

物件,表示控制項中的目前選擇項目。

屬性

範例

下列程式碼範例示範如何使用 SelectedIndexChanged 事件來搜尋並選取不同 ListBox 控制項中的專案。 此範例會 SelectedIndexChanged 使用 事件來判斷 中 ListBox 選取的專案何時變更。 範例程式碼接著會使用 SelectedItem 屬性讀取專案的文字,並使用第一 ListBoxListBox 中傳 SelectedItem 回的文字,在不同的 上呼叫 FindString 方法。 如果在另 ListBox 一個 中找到專案,則會選取該專案。 這個範例要求兩 ListBox 個名為 listBox1listBox2 的控制項已加入表單中,而且這兩 ListBox 個控制項都包含相同的專案。 此範例也需要在範例中定義的事件處理方法連接到 SelectedIndexChangedlistBox1 事件。

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 選取的專案。 SelectionMode如果 的 ListBox 屬性設定為 SelectionMode.MultiSimpleSelectionMode.MultiExtended (,表示多重選取 ListBox) 且清單中已選取多個專案,則此屬性可以傳回任何選取的專案。

若要擷取集合,其中包含多重選取範圍中的所有選取 ListBox 專案,請使用 SelectedItems 屬性。 如果您想要在 中 ListBox 取得目前選取專案的索引位置,請使用 SelectedIndex 屬性。 此外,您可以使用 SelectedIndices 屬性來取得多重選取範圍中的所有選取 ListBox 索引。

適用於