Combobox selectedvalue and selectedindex change unexpectedly

Posted on 2009-12-19
Last Modified: 2012-05-08
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??
Question by:NoraWil
    LVL 7

    Expert Comment

    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.
    LVL 14

    Accepted Solution

    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

    Author Comment

    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


    Author Comment

    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.
    LVL 15

    Assisted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now