Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从另一个组合框填充组合框_Vb.net - Fatal编程技术网

Vb.net 从另一个组合框填充组合框

Vb.net 从另一个组合框填充组合框,vb.net,Vb.net,我有两个组合框;一个包含国家列表,另一个包含城市列表。我如何设置它,以便当您选择一个国家时,该国家的城市在另一个组合框中可见 我想这基本上是基于第一个框的选定值为第二个框创建项集合 编辑:我在找这样的东西: If cboCountry.Text = "Australia" Then cboCity.Collection("Melbourne, "Sydney") End If 编辑 正在进行编辑,我已更改了代码,这应该是您要查找的:) 将其放入组合框1 selected value chan

我有两个组合框;一个包含国家列表,另一个包含城市列表。我如何设置它,以便当您选择一个国家时,该国家的城市在另一个组合框中可见

我想这基本上是基于第一个框的选定值为第二个框创建项集合

编辑:我在找这样的东西:

If cboCountry.Text = "Australia" Then
 cboCity.Collection("Melbourne, "Sydney")
End If
编辑

正在进行编辑,我已更改了代码,这应该是您要查找的:)

将其放入组合框1 selected value changed事件中,应该可以正常工作

          Private Sub cboCountry_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboCountry.SelectedValueChanged

            If cboCountry.Text = "England" Then
               cboCity.Items.Add("London")
            End If

          End Sub

将数据加载到一个
字典(字符串,列表(字符串))
,其中包含从国家到城市的映射

然后,只需在字典中查找所选国家并迭代其值

下面是一个如何完成后一部分的示例。这假设您已经加载了字典数据(显然不要硬编码代码中的值):

如果您只是想用一些玩具数据来测试这一点,以下是一些:

Private cities As New Dictionary(Of String, List(Of String))() From { _
    {"England", New List(Of String)() From {"London", "Darthmouth", "Oxford", "Cambridge"}}, _
    {"Wales", New List(Of String)() From {"Cardiff", "Swansea"}}, _
    {"Scotland", New List(Of String)() From {"Edinburgh", "Glasgow", "Aberdeen"}} _
}

这是一个糟糕的解决方案。它会使代码膨胀,数据本来就不属于代码,而且一旦您有多个城市/国家,或者当您有在运行时动态加载的数据时,这种方法就会失败。最后,即使是在简单的情况下,它也会变得不必要的复杂。这远不是一个简单的解决方案。您的答案并没有真正显示解决方案,它只是显示了单个国家的基本代码(甚至不完整)。请继续,只为欧盟国家及其地区首府写一份完整的报告(保持简单)。好吧,如果你读到的问题是他打算怎么做……而不是我。
' In the citiesCombo.SelectedValueChanged event of the combo box:
cboCity.Items.Clear()
For Each city As var In cities(cboCountry.Text)
    cboCity.Items.Add(city)
Next
Private cities As New Dictionary(Of String, List(Of String))() From { _
    {"England", New List(Of String)() From {"London", "Darthmouth", "Oxford", "Cambridge"}}, _
    {"Wales", New List(Of String)() From {"Cardiff", "Swansea"}}, _
    {"Scotland", New List(Of String)() From {"Edinburgh", "Glasgow", "Aberdeen"}} _
}