Solved

Combobox selectedindex not changing item

Posted on 2014-01-23
6
1,180 Views
Last Modified: 2014-01-28
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.
0
Comment
Question by:MattEtiquette
6 Comments
 
LVL 33

Expert Comment

by:sarabande
ID: 39806096
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.

Sara
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39806152
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?
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39807426
Did you have Autopostback set to true in the combobox?? You can try with

Combobox.Items.FindByValue("ValueToLookUp").Selected=True
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:MattEtiquette
ID: 39811644
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)

        Me.InitializeComponent()
        zForm = aform

    End Sub



                        Try
                            zForm.Contact_nameComboBox.Items.Clear()

                            For Each CTac In ContData.contacts
                                If CTac.org_id = CInt(Me.OrgIdDummyTextBox.Text) Then
                                    zForm.Contact_nameComboBox.Items.Add(CTac.MainName)
                                End If
                                If CTac.contact_id = Me.Contact_idTextBox.Text Then
                                    CurCont = CTac.MainName
                                End If
                            Next
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        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

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39813556
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.
0
 

Author Comment

by:MattEtiquette
ID: 39814456
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

867 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

26 Experts available now in Live!

Get 1:1 Help Now