本文介绍了如何根据第一个组合框选择值将项目添加到第二个组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态地向combobox1添加了产品名称,并且我想动态地在combobox2中添加该产品下的版本,当用户选择与该产品版本对应的一个产品时必须显示。我怎么能实现这个?



我尝试过:



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:

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

推荐答案

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


这篇关于如何根据第一个组合框选择值将项目添加到第二个组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 20:15