在vb.net的两个组合框中以表格形式列出所有国家/地区
我在表单中添加了三个组合框,并在表单加载事件中为每个组合框调用了上述函数三次。 比如: 列表国家(组合框1) 列表国家(组合框2) 列表国家(组合框3) 但第一个组合框只列出所有国家,其他两个为空。请帮我解决这个问题 im使用vb.net 12 ultimate和windows 7在vb.net的两个组合框中以表格形式列出所有国家/地区,vb.net,Vb.net,我在表单中添加了三个组合框,并在表单加载事件中为每个组合框调用了上述函数三次。 比如: 列表国家(组合框1) 列表国家(组合框2) 列表国家(组合框3) 但第一个组合框只列出所有国家,其他两个为空。请帮我解决这个问题 im使用vb.net 12 ultimate和windows 7 谢谢为什么不返回国家/地区列表对象并使用数据源将绑定到每个组合框 当countrylist不包含需要清除的countrylist时,项目也会添加到comboxbox中。它应该是comboxbox.Items.Cont
谢谢为什么不返回国家/地区列表对象并使用数据源将绑定到每个组合框
当countrylist不包含需要清除的countrylist时,项目也会添加到comboxbox中。它应该是comboxbox.Items.Contains()countryList字典对于您的类是全局的,并且在调用此方法之前在某个地方对其进行了初始化。因此,第一个调用发现dictionary为空并将infos添加到dictionary和combo中,但第二个调用(和第三个调用)发现dictionary已填充,因此不会向第二个(和第三个)combo中添加任何内容 如果每次调用此方法时都不重新创建字典,则可以编写
Imports System.Collections.Generic
Imports System.Globalization
Public Sub ListCountries(SourceCombo As System.Windows.Forms.ComboBox)
' Iterate the Framework Cultures...
For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
Dim ri As RegionInfo
Try
ri = New RegionInfo(ci.Name)
Catch
'If a RegionInfo object could not be created don't use the CultureInfo for the country list.
Continue For
End Try
' Create new country dictionary entry.
Dim newKeyValuePair As New KeyValuePair(Of String, String)(ri.EnglishName, ri.ThreeLetterISORegionName)
' If the country is not already in the countryList add it...
If Not countryList.ContainsKey(ri.EnglishName) Then
countryList.Add(newKeyValuePair.Key, newKeyValuePair.Value)
SourceCombo.Items.Add(ri.EnglishName)
End If
Next
SourceCombo.Sorted = True
End Sub
您知道这段代码是有效的,因为它知道如何填充至少一个组合框。我们看不到不起作用的代码。在输出窗口中查找“首次机会异常”通知。注意。您有一个全局字典实例,第二个调用被跳过,因为上一个调用已经填充了字典。但是跳过添加到字典,您也会跳过组合框中的插入。有其他方法吗?当我使用您的代码运行表单时,所有组合框中都会出现一个字符串“(Collections)”。没有添加任何项目。它是空的,真奇怪。我已经测试了代码,它的工作原理是这样的。你能告诉我如何调用这个代码吗?Listcountries(ComboBox1)我的程序包含一个MDI表单和子表单。这是我在子表单中使用的代码。当我在MDI表单下执行此表单时,我收到了相同的结果&单独执行。我不知道该说什么。如果表单是MDI子表单或不是MDI子表单,请检查是否存在差异,因为此上下文中没有差异
Dim countryList as SortedDictionary(Of string, String)
Public Sub ListCountries(SourceCombo As System.Windows.Forms.ComboBox)
If countryList Is Nothing Then
countryList = BuildCountryList()
End If
SourceCombo.DisplayMember = "Key"
SourceCombo.ValueMember = "Value"
SourceCombo.DataSource = New BindingSource(countryList, Nothing)
' No need to sort anything
End Sub
Public Function BuildCountryList() As SortedDictionary(Of String, String)
Dim temp = New SortedDictionary(Of String, String)
For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.AllCultures)
Dim ri As RegionInfo
Try
ri = New RegionInfo(ci.Name)
Catch
'If a RegionInfo object could not be created don't use the CultureInfo for the country list.
Continue For
End Try
' If the country is not already in the countryList add it...
If Not temp.ContainsKey(ri.EnglishName) Then
temp.Add(ri.EnglishName, ri.ThreeLetterISORegionName)
End If
Next
Return temp
End Function