ListBox.SelectedIndexChanged ListBox.SelectedIndexChanged ListBox.SelectedIndexChanged ListBox.SelectedIndexChanged Event

定义

SelectedIndex 属性或 SelectedIndices 集合更改后发生。Occurs when the SelectedIndex property or the SelectedIndices collection has changed.

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

示例

下面的代码示例演示如何使用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使用属性读取项的文本, 并使用第一个ListBox SelectedItemFindString返回的文本在ListBox另一个上调用方法。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. 此示例要求在窗ListBox体中添加listBox1listBox2两个名为和的控件, 并且这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. 该示例还要求在示例中定义的事件处理方法已连接到SelectedIndexChangedlistBox1事件。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的选定索引何时发生更改。You can create an event handler for this event to determine when the selected index in the ListBox has been changed. 当你需要基于中ListBox的当前选定内容在其他控件中显示信息时, 这会很有用。This can be useful when you need to display information in other controls based on the current selection in the ListBox. 您可以使用此事件的事件处理程序将信息加载到其他控件中。You can use the event handler for this event to load the information in the other controls.

如果将SelectionMode.MultiSimple SelectionMode.MultiExtended属性设置为或, 则对SelectedIndices集合所做的任何更改 (包括从选定内容中删除项) 都将引发此事件。 SelectionModeIf the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, any change to the SelectedIndices collection, including removing an item from the selection, will raise this event.

有关处理事件的详细信息, 请参阅处理和引发事件For more information about handling events, see Handling and Raising Events.

适用于

另请参阅