2
votes

I have two comboboxes. If the user selects a certain value from the first combobox then I want the second combobox to automatically change and select to its corresponding value.

For example:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedValue = 3 Then 
     Me.WeightComboBox1.SelectedValue = 2

I have also tried:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedText = "Internal Guidelines" Then                 
    Me.WeightComboBox1.SelectedText = "None"

Can anyone help?

2
Have you stepped through the code to see what SelectedValue equals? My guess is that you did not set the ValueMemeber to the thing you think you did.Steve
What's in the box? If they are just strings, try SelectedItem. SelectedText is for the actual highlighted text in the displayed string.LarsTech
Do the combo boxes have any items added to them prior to these lines of codes? If you do "selected text = something" that something should be added to the items prior to that line of code.Kat
@LarsTech I tried SelectedItem and got an error stating "Conversion string "Internal Guidelines" to type Double is not valid"AznDevil92
@sparkysword Yes the combobox have items added prior to the code. What I did to populate the comboboxes I had to write a SQL query first since its pulling from my database and compose a code using For i ....items.add(sqltable.Rows(i)(...)AznDevil92

2 Answers

3
votes
//selected index changed event for the combo box 

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles InstitutionAdvisoryFirmsComboBox1.SelectedIndexChanged

    If Me.InstitutionAdvisoryFirmsComboBox1.SelectedIndex = 3 Then 
        If Me.WeightComboBox1.Items.Count >= 2 Then // make sure you are not accessing an index out of range
            Me.WeightComboBox1.SelectedIndex = 2
        End If
    End If

End Sub 
2
votes

You will need to use the event handler for the comboBox that you are checking. Inside the event handler you can use a slight variation of your if statement:

If Me.InstitutionAdvisoryFirmsComboBox1.SelectedIndex = 1 Then                 
    Me.WeightComboBox1.SelectedIndex = 2

When using the SelectedIndex function, the counting starts at the 0th position.

When Using SelectedValue or SelectedItem is used for getting that text or value inside the combobox.

Dim ComboValue As String = comboBox1.SelectedValue.ToString();

or

Dim ComboValue As String = comboBox1.SelectedItem.ToString();