ListBox.SelectedIndexChanged 이벤트

정의

SelectedIndex 속성이나 SelectedIndices 컬렉션이 변경될 때 발생합니다.

public:
 event EventHandler ^ SelectedIndexChanged;
public event EventHandler SelectedIndexChanged;
public event EventHandler? SelectedIndexChanged;
member this.SelectedIndexChanged : EventHandler 
Public Custom Event SelectedIndexChanged As EventHandler 

이벤트 유형

예제

다음 코드 예제에서는 검색 하 고 다른 ListBox 컨트롤에서 SelectedIndexChanged 항목을 선택 하는 이벤트를 사용 하는 방법을 보여 줍니다. 이 예제에서는 SelectedIndexChanged 이벤트를 사용하여 에서 ListBox 선택한 항목이 변경되는 시기를 결정합니다. 그런 다음 예제 코드는 속성을 사용하여 SelectedItem 항목의 텍스트를 읽고 첫 번째 ListBox에서 에서 반환된 SelectedItem 텍스트를 사용하여 다른 ListBox 에서 메서드를 호출 FindString 합니다. 다른 ListBox에 항목이 있으면 항목이 선택됩니다. 이 예제에서는 및 listBox2라는 listBox1 두 개의 ListBox 컨트롤이 양식에 추가되고 두 컨트롤에 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.MultiExtendedSelectionMode.MultiSimple 설정된 경우 선택 영역에서 항목을 제거하는 것을 포함하여 컬렉션에 대한 변경 SelectedIndices 내용이 이 이벤트를 발생합니다.

이벤트 처리에 대한 자세한 내용은 이벤트 처리 및 발생 을 참조하십시오.

적용 대상

추가 정보