.NET Framework Class Library
ListBox.SelectedIndexChanged Event

Occurs when the SelectedIndex property or the SelectedIndices collection has changed.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Event SelectedIndexChanged As EventHandler
Visual Basic (Usage)
Dim instance As ListBox
Dim handler As EventHandler

AddHandler instance.SelectedIndexChanged, handler
C#
public event EventHandler SelectedIndexChanged
Visual C++
public:
 event EventHandler^ SelectedIndexChanged {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
JScript
JScript does not support events.

You can create an event handler for this event to determine when the selected index in the ListBox has been changed. 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.

If 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 Consuming Events.

The following code example demonstrates how to use the SelectedIndexChanged event to search for and select an item in a different ListBox control. The example uses the SelectedIndexChanged event to determine when the selected item in the ListBox is changed. 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. If an item is found in the other ListBox, the item is selected. 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. The example also requires that the event-handling method defined in the example is connected to the SelectedIndexChanged event of listBox1.

Visual Basic
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

C#
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);
}

Visual C++
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 );
   }

J#
private void listBox1_SelectedIndexChanged(Object sender, 
    System.EventArgs e)
{
    // Get the currently selected item in the ListBox.
    String curItem = listBox1.get_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);
    }
} //listBox1_SelectedIndexChanged 

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0


MSDN Library
.NET Development
.NET Framework 3.5
.NET Framework Class Library
System.Windows.Forms Namespace
ListBox Class
ListBox Events
SelectedIndexChanged Event
Tags: 
 
Community Content
OSZAR »