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 항목의 텍스트를 읽고 첫 번째 ListBox에서 에서 반환된 SelectedItem 텍스트를 사용하여 다른 ListBox 에서 메서드를 호출 FindString 합니다. 다른 ListBox에 항목이 있으면 항목이 선택됩니다. 이 예제에서는 및 listBox2라는 listBox1ListBox 컨트롤이 폼에 추가되었으며 두 컨트롤 모두 ListBox 동일한 항목을 포함해야 합니다. 이 예제에서는 예제에 정의된 이벤트 처리 메서드가 의 listBox1이벤트에 연결되어야 합니다SelectedIndexChanged.

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 속성이 또는 SelectionMode.MultiExtended (다중 선택ListBox)로 설정 SelectionMode.MultiSimple 되고 목록에서 여러 항목이 선택된 경우 이 속성은 선택한 항목을 반환 ListBox 할 수 있습니다.

여러 선택 ListBox영역에서 선택한 모든 항목이 포함된 컬렉션을 검색하려면 속성을 사용합니다 SelectedItems . 에서 현재 선택한 항목 ListBox의 인덱스 위치를 가져오려면 속성을 사용합니다 SelectedIndex . 또한 속성을 사용하여 SelectedIndices 다중 선택 ListBox에서 선택한 모든 인덱스를 가져올 수 있습니다.

적용 대상