This documentation is archived and is not being maintained.

ComboBox.SelectedIndexChanged Event

Occurs when the SelectedIndex property has changed.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public Event SelectedIndexChanged As EventHandler

You can create an event handler for this event to determine when the selected index in the ComboBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ComboBox. You can use the event handler for this event to load the information in the other controls.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to initialize a ComboBox control by setting the MaxDropDownItems and DropDownStyle properties and using the FindStringExact methods to search the ComboBox. It also shows handling the SelectedIndexChanged event.

Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label1.Location = New System.Drawing.Point(8, 24)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(120, 32)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Use drop-down to choose a name:"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Name = "Form1"
        Me.Text = "Form1"
    End Sub

    Shared Sub Main()
        Application.Run(New Form1)
    End Sub

    Friend WithEvents Label1 As System.Windows.Forms.Label

    ' Declare and initialize the text box.
    ' This text box text will be update programmatically. The user is not 
    ' allowed to update it, so the ReadOnly property is set to true.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    Private Sub InitializeTextBox()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox1.ScrollBars = ScrollBars.Vertical
        Me.TextBox1.Location = New System.Drawing.Point(64, 128)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ReadOnly = True
        Me.TextBox1.Size = New System.Drawing.Size(184, 120)
        Me.TextBox1.TabIndex = 4
        Me.TextBox1.Text = "Employee and Number of Awards:"
    End Sub

    ' Declare comboBox1 as a ComboBox.
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox

    ' This method initializes the combo box, adding a large string 
    ' array but limiting the drop-down size to six rows so the combo box
    ' doesn't cover other controls when it expands.
    Private Sub InitializeComboBox()
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Dim employees() As String = New String() {"Hamilton, David", _
            "Hensien, Kari", "Hammond, Maria", "Harris, Keith", _
            "Henshaw, Jeff D.", "Hanson, Mark", "Harnpadoungsataya, Sariya", _
            "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", _
            "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", _
            "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Harris, Keith", "Henshaw, Jeff D.", _
            "Hensien, Kari", "Hasselberg, Jonas", "Harrington, Mark", _
            "Hedlund, Magnus", "Hay, Jeff", "Heidepriem, Brandon D."}

        Me.ComboBox1.Location = New System.Drawing.Point(136, 32)
        Me.ComboBox1.IntegralHeight = False
        Me.ComboBox1.MaxDropDownItems = 5
        Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(136, 81)
        Me.ComboBox1.TabIndex = 0
    End Sub

    ' This method is called when the user changes his or her selection.
    ' It searches for all occurrences of the selected employee's
    ' name in the Items array and adds the employee's name and 
    ' the number of occurrences to TextBox1.Text.

    ' CAUTION   This code exposes a known bug: If the index passed to the 
    ' FindStringExact(searchString, index) method is the last index 
    ' of the array, the code throws an exception.
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        Dim comboBox As comboBox = CType(sender, comboBox)

        ' Save the selected employee's name, because we will remove
        ' the employee's name from the list.
        Dim selectedEmployee = CType(ComboBox1.SelectedItem, String)

        Dim count As Integer = 0
        Dim resultIndex As Integer = -1

        ' Call the FindStringExact method to find the first 
        ' occurrence in the list.
        resultIndex = ComboBox1.FindStringExact(ComboBox1.SelectedItem)

        ' Remove the name as it is found, and increment the found count. 
        ' Then call the FindStringExact method again, passing in the index of the
        ' current found item so the search starts there instead of 
        ' at the beginning of the list.
        While (resultIndex <> -1)
            count += 1
            resultIndex = ComboBox1.FindStringExact _
            (selectedEmployee, resultIndex)
        End While

        ' Update the text in Textbox1.
        TextBox1.Text = TextBox1.Text & Microsoft.VisualBasic.vbCrLf _
            & selectedEmployee & ": " & count
    End Sub

End Class

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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