问题
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