0
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

I added three combo boxes to a form and called the above function three times for each combo boxes in the form load event. like: listcountries(ComboBox1) listcountries(ComboBox2) listcountries(ComboBox3)

but the first combobox only lists all countries and the other two are empty. please help me how to solve this.

im using vb.net 12 ultimate & windows 7

thank you

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Well, you know that this code works since it knows how to populate at least one combobox. We can't see the code that does not work. Look in the Output window for a "first chance exception" notification. And watch out for [this nasty Windows 7 bug](http://stackoverflow.com/a/4934010/17034). – Hans Passant Jan 20 '16 at 12:59
  • You have a global dictionary instance and the second call is skipped because the dictionary has been filled by the previous call. But skipping the Add to the dictionary you skip also the insert in the combobox. – Steve Jan 20 '16 at 13:04
  • any alternative ways? – Mohamed Kiyas Jan 21 '16 at 08:59

2 Answers2

0

Why not return the country list object and bind the to each combobox using datasource?

Also items are added to comboxbox when countrylist doesnt contains that so need to clear countrylist. It should be comboxbox.Items.Contains()

Mahesh Malpani
  • 1,782
  • 16
  • 27
0

The countryList dictionary is global to your class and it is initialized somewhere before calling this method. So the first call finds the dictionary empty and adds the infos both to the dictionary and the combo, but the second call (and the third one) find the dictionary already filled and thus doesn't add anything to the second (and third) combo

Without recreating the dictionary every time you call this method you could write

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
Steve
  • 213,761
  • 22
  • 232
  • 286
  • When I run the form with your codes, an string "(Collections)" appears in all the comboboxes. No any items added. Its empty. – Mohamed Kiyas Jan 21 '16 at 08:55
  • That's strange. I have tested the code and it works as is. Can you show how do you call this code? – Steve Jan 21 '16 at 09:17
  • Listcountries(ComboBox1) – Mohamed Kiyas Jan 21 '16 at 09:49
  • My program contains an MDI form & child forms. This codes I used in a child form. The same result i received when I executed this form under the MDI form & alone. – Mohamed Kiyas Jan 21 '16 at 10:17
  • I don't know what to say. Please check if something is different because there is no difference in this context if the form is an MDI child form or not – Steve Jan 21 '16 at 14:35