Click here to Skip to main content
15,877,675 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have added product names dynamically to combobox1 and i want to add versions coming under that product in combobox2 dynamically, when user selects one product corresponding to that product versions must be displayed. how can i implement this??

What I have tried:

VB
Public Sub add_Control()
        Dim dict As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))
        Dim dic As Dictionary(Of String, List(Of String))
        For Each pList As product In productArrayList  //productArraylist contains my product names and version
            Dim l As List(Of String) = New List(Of String)
            Form1.ComboBox1.Items.Add(pList.ProductName)
           
            ' Form1.ComboBox2.Items.Add(pList.ProductVersion)
            For i = 0 To Form1.ComboBox1.Items.Count - 2 Step 1
                For j = Form1.ComboBox1.Items.Count - 1 To i + 1 Step -1
                    If Form1.ComboBox1.Items(i).ToString = Form1.ComboBox1.Items(j).ToString Then
                        Form1.ComboBox1.Items.RemoveAt(j)
                        dict.
                        dic = dict.GetDictionary("MyDictionary", True)
                        'Add some values 
                        dic.LongSetAt(2, "two")    //this method is nt working
                        dic.LongSetAt(15, "fifteen")
                        dic.LongSetAt(300, "three hundred")

                        'Form1.ComboBox2.Items.Add(pList.ProductVersion)
                    Else
                        '    Form1.ComboBox1.Items.Add(pList.ProductName)
                        l.Add(pList.ProductVersion)
                        dict.Add(pList.ProductName, l)
                    End If
                Next
            Next
            ' Form1.ComboBox2.Items.Add(pList.ProductVersion)
                   Next
    End Sub
Posted
Updated 28-Feb-17 21:25pm
v2

1 solution

VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i As Integer = 1 To 10

        Dim versions As List(Of String) = New List(Of String)()
        Dim key As String = "Item " + CStr(i)
        For j As Integer = 1 To 5
            versions.Add(CStr(i) + ".0" + CStr(j))
        Next
        dic.Add(key, versions)
        ComboBox1.Items.Add(key)
    Next
End Sub

Dim dic As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))()

Private Sub ComboBox1_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedValueChanged
    Dim versions As List(Of String) = dic(ComboBox1.SelectedItem)
    ComboBox2.SelectedIndex = -1
    ComboBox2.Items.Clear()
    For i As Integer = 0 To versions.Count - 1
        ComboBox2.Items.Add(versions(i))
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900