0

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

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

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

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

2 回答 2

1

将数据加载到Dictionary(Of String, List(Of String))包含从国家到城市的映射中。

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

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

' As a private Form variable:
Private cities As New Dictionary(Of String, List(Of String))()
' … Load data in Form_Load.
' 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"}} _
}
于 2012-08-28T15:50:53.507 回答
0

编辑

继续你的编辑,我已经改变了代码,这应该是你要找的:)

将其放入组合框 1 选定值更改事件中,并且应该可以工作。

          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
于 2012-08-28T15:16:47.597 回答