Hi,

Thank you all for your help.

I wrote this code and I want (Combobox2) to show only the cities that are selected from (Combobox1)

Example:
When (Michigan) is selected from (Combobox1) - (Detroit & Ann Arbor) are the only choices that should be selected from (Combobox2)
And When (Ohio) is selected from (Combobox1) - (Cleveland and Dayton) are the only choices that should be selected from (Combobox2)

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.ComboBox1.Items.Add("Michigan")
        Me.ComboBox1.Items.Add("Ohio")
        Me.ComboBox2.Items.Add("Detroit")
        Me.ComboBox2.Items.Add("Ann Arbor")
        Me.ComboBox2.Items.Add("Cleveland")
        Me.ComboBox2.Items.Add("Dayton")
 
    End Sub

Well if you want the other options to appear, but be disabled then I don't think that is easy task because of the nature of the control. Look at this: Hide item in combobox

Otherwise I think you can do it this way:

Select Case ComboBox1.SelectedItem
            Case "Michigan"
                ComboBox2.Items.Clear()
                ComboBox2.Text = ""
                ComboBox2.Items.Add("Detroit")
                ComboBox2.Items.Add("Ann Arbor")
            Case "Ohio"
                ComboBox2.Items.Clear()
                ComboBox2.Text = ""
                ComboBox2.Items.Add("Cleveland")
                ComboBox2.Items.Add("Dayton")
        End Select

With that, it would only be necessary to put only the following in the Form1_Load event:

Me.ComboBox1.Items.Add("Michigan")
        Me.ComboBox1.Items.Add("Ohio")

Thanks for the quick reply,

but when I start debugging the code provided, I get the text in (Combobox1) and nothing is shown in (Combobox2) to be selected ?

Thanks for the quick reply,

but when I start debugging the code provided, I get the text in (Combobox1) and nothing is shown in (Combobox2) to be selected ?

I see. Try to put it on the ComboBox_SelectedIndexChange event. In other words, right inside of:

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

'Code would go here

End Sub

If you already have it that way and it still doesn't work, then change each ComboBox word to Me.ComboBox to make sure it applies to the current form. It would look like this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Select Case Me.ComboBox1.SelectedItem
            Case "Michigan"
                Me.ComboBox2.Items.Clear()
                Me.ComboBox2.Text = ""
                Me.ComboBox2.Items.Add("Detroit")
                Me.ComboBox2.Items.Add("Ann Arbor")
            Case "Ohio"
                Me.ComboBox2.Items.Clear()
                Me.ComboBox2.Text = ""
                Me.ComboBox2.Items.Add("Cleveland")
                Me.ComboBox2.Items.Add("Dayton")
        End Select
    End Sub

If that doesn't work make sure you are referring to the right ComboBox, in other words, make sure that the other ComboBox is named ComboBox2 and if its not, change the code accordingly to its name. Though, I think you are doing this last part correctly because otherwise you wouldn't be able to debug without getting an error warning.

It works. Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.