Answered by:
Drop Down List Box

Question
-
I am trying to use one drop down list box to drive another boxes options. For example if I choose #1 in the first drop down list box, then my options would be "A, B, C", if I choose #2 my options would be "D, E, F", if I choose #3 my choices would be "A,B,C,D,E,F". How can I accomplish this?
Thursday, May 29, 2008 2:31 PM
Answers
-
I have not understood if you want to do it for the same combobox or for another one however here there is a simple code:
Code SnippetPrivate Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedItem = "1" Then
Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("A") Me.ComboBox1.Items.Add("B") Me.ComboBox1.Items.Add("C") ElseIf Me.ComboBox1.SelectedItem = "2" Then Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("D") Me.ComboBox1.Items.Add("E") Me.ComboBox1.Items.Add("F") ElseIf Me.ComboBox1.SelectedItem = "3" Then Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("A") Me.ComboBox1.Items.Add("B") Me.ComboBox1.Items.Add("C") Me.ComboBox1.Items.Add("D") Me.ComboBox1.Items.Add("E") Me.ComboBox1.Items.Add("F") End If End SubBest regards,
Ale N.
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 2:58 PM -
Ale N. is correct that you can use the SelectedIndexChanged event handler to determine when teh selected index of the list has changed. I might also point out that the SelectionChangeCommitted event may be more appropriate if you need to only trap index changes that the user has initiated (as opposed to you doing in programmatically in your code).
I think you meant for another list (i.e. Combobox2) to change instead of the same combobox -- if the above code was used as-is you would never get 1,2, or 3 in the list after the first time one of those was chosen, so it is not terribly proctical (without some way to re-reset the list). We may be able to find a better solution for you -- perhaps one that is not statically hard-coded -- if you can explain the relationship between the two lists you want to work with and maybe even supply some code as to where you get the values from. The idea here is to explain how the code should know that selecteing "1" means filtering list 2 to just A,B,C.
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 3:22 PM -
I would use something like this....
Code SnippetDim valueSet As Dictionary(Of Int32, String()) = New Dictionary(Of Int32, String())
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub
valueSet.Add(1,
valueSet.Add(2,
New String() {"D", "E", "F"})valueSet.Add(3,
New String() {"A", "B", "C", "D", "E", "F"})ComboBox1.Items.AddRange(
New Object() {1, 2, 3}) End SubComboBox2.DataSource = valueSet(
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 4:17 PM
All replies
-
I have not understood if you want to do it for the same combobox or for another one however here there is a simple code:
Code SnippetPrivate Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If Me.ComboBox1.SelectedItem = "1" Then
Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("A") Me.ComboBox1.Items.Add("B") Me.ComboBox1.Items.Add("C") ElseIf Me.ComboBox1.SelectedItem = "2" Then Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("D") Me.ComboBox1.Items.Add("E") Me.ComboBox1.Items.Add("F") ElseIf Me.ComboBox1.SelectedItem = "3" Then Me.ComboBox1.Items.Clear() Me.ComboBox1.Items.Add("A") Me.ComboBox1.Items.Add("B") Me.ComboBox1.Items.Add("C") Me.ComboBox1.Items.Add("D") Me.ComboBox1.Items.Add("E") Me.ComboBox1.Items.Add("F") End If End SubBest regards,
Ale N.
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 2:58 PM -
Ale N. is correct that you can use the SelectedIndexChanged event handler to determine when teh selected index of the list has changed. I might also point out that the SelectionChangeCommitted event may be more appropriate if you need to only trap index changes that the user has initiated (as opposed to you doing in programmatically in your code).
I think you meant for another list (i.e. Combobox2) to change instead of the same combobox -- if the above code was used as-is you would never get 1,2, or 3 in the list after the first time one of those was chosen, so it is not terribly proctical (without some way to re-reset the list). We may be able to find a better solution for you -- perhaps one that is not statically hard-coded -- if you can explain the relationship between the two lists you want to work with and maybe even supply some code as to where you get the values from. The idea here is to explain how the code should know that selecteing "1" means filtering list 2 to just A,B,C.
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 3:22 PM -
I would use something like this....
Code SnippetDim valueSet As Dictionary(Of Int32, String()) = New Dictionary(Of Int32, String())
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadData()
End Sub
valueSet.Add(1,
valueSet.Add(2,
New String() {"D", "E", "F"})valueSet.Add(3,
New String() {"A", "B", "C", "D", "E", "F"})ComboBox1.Items.AddRange(
New Object() {1, 2, 3}) End SubComboBox2.DataSource = valueSet(
- Marked as answer by Martin Xie - MSFT Monday, June 2, 2008 2:28 AM
Thursday, May 29, 2008 4:17 PM