locked
Drop Down List Box RRS feed

  • 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 Snippet

    Private 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 Sub

     

     

    Best regards,

     

    Ale N.

    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.

     

    Thursday, May 29, 2008 3:22 PM
  • I would use something like this....

     

    Code Snippet

    Dim 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

     

    Private Sub LoadData()

    valueSet.Add(1, New String() {"A", "B", "C"})

    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 Sub

     

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

    ComboBox2.DataSource = valueSet(CType(ComboBox1.SelectedItem, Int32))

    End Sub

     

     

    Good Luck!
    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 Snippet

    Private 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 Sub

     

     

    Best regards,

     

    Ale N.

    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.

     

    Thursday, May 29, 2008 3:22 PM
  • I would use something like this....

     

    Code Snippet

    Dim 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

     

    Private Sub LoadData()

    valueSet.Add(1, New String() {"A", "B", "C"})

    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 Sub

     

    Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted

    ComboBox2.DataSource = valueSet(CType(ComboBox1.SelectedItem, Int32))

    End Sub

     

     

    Good Luck!
    Thursday, May 29, 2008 4:17 PM