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

속성 값

Object

컨트롤의 현재 선택 항목을 나타내는 개체입니다.

특성

예제

다음 코드 예제에서는 검색 하 고 다른 ListBox 컨트롤에서 SelectedIndexChanged 항목을 선택 하는 이벤트를 사용 하는 방법을 보여 줍니다. 이 예제에서는 이벤트를 사용하여 SelectedIndexChanged 선택한 항목 ListBox 이 변경되는 시기를 결정합니다. 그런 다음 예제 코드는 속성을 사용하여 SelectedItem 항목의 텍스트를 읽고 첫 번째 ListBox코드에서 반환된 SelectedItem 텍스트를 사용하여 다른 ListBox 메서드를 호출 FindString 합니다. 다른 ListBox항목에 항목이 있으면 해당 항목이 선택됩니다. 이 예제에서는 명명 listBox1 된 두 컨트롤 ListBoxlistBox2폼에 추가되고 두 컨트롤에 ListBox 동일한 항목이 포함되어야 합니다. 이 예제에서는 예제에 정의된 이벤트 처리 메서드가 이벤트에 연결되어야 합니다 SelectedIndexChanged 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선택한 항목을 확인할 수 있습니다. 속성 ListBoxSelectionMode 목록에서 여러 항목 중 하나 SelectionMode.MultiSimple 또는 SelectionMode.MultiExtended (다중 선택 항목을 ListBox나타낸)로 설정되어 있고 여러 항목이 선택된 경우 이 속성은 선택한 항목을 반환할 수 있습니다.

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

적용 대상