Parent/Child ComboBoxes

若如初见. 提交于 2021-01-29 20:30:42

问题


I have 2 combobox, each having different items but somehow it's a hierarchy of characteristics of the next combobox ie: Combobox1 has VEHICLES, MOTORBIKES and NONE as items. I want to be able to choose Vehicles in combobox1 and Combobox2 updates with items only associated with vehicles ie Sportscar, Sedan etc. The same should happen when I choose Motorcycles in combobox1, it should update combobox2 with items only related to motorcycles.


回答1:


Put this sample code inside the ComboBox1_TextChanged Event for you to have reference.

Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged

        ComboBox2.Items.Clear()
        If ComboBox1.Text = "Vehicles" Then
            ComboBox2.Items.Add("Sportscar")
            ComboBox2.Items.Add("Sedan")
        ElseIf ComboBox1.Text = "Motorbikes" Then
            ComboBox2.Items.Add("Harley Davidson")
            ComboBox2.Items.Add("Some motorbikes")
        End If


    End Sub

You can also do this on a Select Case Statement

Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged

        ComboBox2.Items.Clear()

        Select Case ComboBox1.Text
            Case "Vehicles"
                ComboBox2.Items.Add("Sportscar")
                ComboBox2.Items.Add("Sedan")
            Case "Motorbikes"
                ComboBox2.Items.Add("Harley Davidson")
                ComboBox2.Items.Add("Some motorbikes")
            Case Else

        End Select


    End Sub


来源:https://stackoverflow.com/questions/65651213/parent-child-comboboxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!