Combobox selectedvalue and selectedindex change unexpectedly

Hi experts,
In my application users can select a client in a combobox showing the names of our clients.
Selecting a client fires the combobox.SelectedIndexChanged event. There I set the client number (which is a string) this way
strCodeClient = Me.cboClients.SelectedValue.ToString()
This far everythings goes well in all cases. When I click on another control the SelectedIndexChanged event and the SelectedValueChanged event are NOT fired again so strCodeClient remains unchanged.
In our client list there are a clients with the same name. Here my application behaves different. Let's presume there are five clients with the name 'KBC'. When the users selects another KBC than the first one, SelectedIndexChanged and SelectedValueChanged are fired and strCodeClient is set perfectly.
BUT, when the users then clicks on another control, SelectedIndexChanged and SelectedValueChanged are fired again and the SelectedValue and SelectedIndex are set to the FIRST instance of KBC. So is also strCodeClient. In another words the wrong KBC wil be selected.
What am I doing wrong??
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

raja_ind82Connect With a Mentor Commented:
What do you mean by another control? do you have two comboboxes in your form?

Please check the "ComboBox1_SelectedIndexChanged" event handles correctly for the respective controls.

Check the following links:

Raja Muthaiah
When you select the Client in the ComboBox, Trap ony the SelectedValue, rather SelectedIndex. To trap SelectedValue, You should bind SelectedValue as well with the ComboxBox as a Value field.
NoraWilAuthor Commented:
Actually in my appliction I don't use SelectedIndex at all. I used it only to find out what goes wrong.
I only use the SelectedValueChanged event and SelectedValue.
But what do you mean by binding SelectedValue with the Combobox as a Value Field
Now I use the code below to fill The Combobox. In the example above I translated the real names of my controls to english equivalents. Below are the really used named (so cboKlanten in stead of cboClients)

  Private Sub Vul_Klanten()
      Dim strSQL As String = "SELECT Kla_strNaam, Kla_strCodeKlant FROM [Klanten] " & _
                             "ORDER BY [Kla_strNaam]"
      Dim daKlanten As New SqlDataAdapter(strSQL, conRaptor)
      Dim dsKlanten As New DataSet
      daKlanten.Fill(dsKlanten, "Klanten")
        With Me.cboKlanten
          .DataSource = dsKlanten.Tables("Klanten")
          .ValueMember = "Kla_strCodeKlant"
          .DisplayMember = "Kla_strNaam"
        End With
      'MsgBox("Aantal klanten: " & dsKlanten.Tables("Klanten").Rows.Count.ToString())
      Me.cboKlanten.Text = String.Empty
      Me.cboKlanten.BackColor = colRood
  End Sub

Open in new window

NoraWilAuthor Commented:
Thanks for the fast reply. The first link you've sent describes perfectly the problem I'm dealing with. Unfortunally no solution is given. The over all conclusion in the document is: it's a bug in Framework 3.5 and there's no workaround.
I hope there is a workaround.
x77Connect With a Mentor Commented:
ComboBox Events SelectedIndexChanged and SelectedItemChanged, Fires where user select a item and also where Code asign a value to SelectedItem or SelectedIndex and Also if you Bind To a Datasource when show the first item on DataSource.

There are´nt any event like UserSelectItem/Index.
You need declare a Form Variable know when event fires from code action.


Private CodeAction as boolean.

CodeAction = True
MyCombo.DataSource = MyTable
CodeAction = False

    Private Sub MyCombo_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)...
        If CodeAction OrElse MyCombo.SelectedIndex < 0 Then Return

All Courses

From novice to tech pro — start learning today.