Combobox selectedindex not changing item

I have two forms, form1 and form2.  

form1 has a combobox, items filled on form load from a linq to Sql Class.   The combobox has a databinding to a text field in a table.

A button on form1 opens form2 (form1 is passed byref to form2).

in form2 the user enters new data which goes to the linq to Sql table, and the combobox items on form1 are cleared and repopulated so adding the new item to the combobox list.

So far so good.

Changing the selectedindex of the combobox on form1 does not change the value shown in the combobox.

Changing the text in the combobox does not change the value shown in the combobox.

I have established that the selectedindex value I am using is correct.

What is the correct way to get the value in the combobox to change to the new value?

Thanks to any responders.
Who is Participating?
CodeCruiserConnect With a Mentor Commented:
Is the OrderMain already visible by the time you execute this code? If not, try running this code after the form is visible and see if that makes any difference.
for form1 you need to catch the CBN_SELCHANGE event of the combobox control. for form2 the CBN_EDITCHANGE. in the handlers you have to do the needed synchronization.

Setting the SelectedIndex of the combobox to the index of the new value should work. Where have you put this code? Can you show the code you are using to change the selectedindex?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Jesus RodriguezIT ManagerCommented:
Did you have Autopostback set to true in the combobox?? You can try with

MattEtiquetteAuthor Commented:
Some clarification to the above.

The forms are Winforms in a desktop app.

Some code included below.   I am not sure the way I am declaring the form variable is correct.

I have checked that the selected item value is greater than -1 (anum)

    Dim zForm As OrderMain
    Public ContData As New LinqContactDataContext

    Public Sub New(ByRef aform As OrderMain)

        zForm = aform

    End Sub


                            For Each CTac In ContData.contacts
                                If CTac.org_id = CInt(Me.OrgIdDummyTextBox.Text) Then
                                End If
                                If CTac.contact_id = Me.Contact_idTextBox.Text Then
                                    CurCont = CTac.MainName
                                End If
                        Catch ex As Exception
                        End Try

                        aNum = zForm.Contact_nameComboBox.FindStringExact(CurCont)
                        zForm.Contact_nameComboBox.SelectedIndex = zForm.Contact_nameComboBox.FindStringExact(CurCont)
                        zForm.Contact_nameComboBox.Text = CurCont

Open in new window

MattEtiquetteAuthor Commented:
Thanks for the replies.

CodeCruiser pointed me in the right direction, I had some code in the OnActivate event on the OrderMain form that was casuing the problem.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.