filling a combobox with countries
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: filling a combobox with countries

  1. #1
    Join Date
    Mar 2012
    Location
    Zwolle, Netherlands
    Posts
    5

    filling a combobox with countries

    I need to fill a combobox with countries.

    For this I have the following code:

    // fill country combobox with countries
    public List<string> GetCountryList()
    {
    List<string> cultureList = new List<string>();
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

    cultureList.Add("-- Select Country --");

    foreach(CultureInfo culture in cultures)
    {
    // check if it's not a invariant culture
    if(culture.LCID != 127)
    {
    RegionInfo region = new RegionInfo(culture.LCID);
    // add countries that are not in the list
    if(!(cultureList.Contains(region.EnglishName)))
    {
    cultureList.Add(region.EnglishName);
    }
    }
    }
    cultureList.Sort(); // sort alphabetically
    return cultureList;
    }


    Code I found about this on the internet was all a couple years old and made for windows XP, in 7 you get some extra data. so i found the extra check - if(culture.LCID != 127)

    however now it won't compile on
    RegionInfo region = new RegionInfo(culture.LCID);

    giving
    ArgumentException was unhandled
    Culture ID 1 (0x0001) is a neutral culture; a region cannot be created from it.
    Parameter name: culture

    and to be exact this is the 2nd neutral culture it comes across. So why does it get the 1st, and not the 2nd?

    but most important how can I solve this?

  2. #2
    Join Date
    Mar 2012
    Location
    Zwolle, Netherlands
    Posts
    5

    Re: filling a combobox with countries

    Sorry forgot the tags...
    as I can't change post I'll repost this for better reading.

    I need to fill a combobox with countries.

    For this I have the following code:

    Code:
            // fill country combobox with countries
            public List<string> GetCountryList()
            {
                List<string> cultureList = new List<string>();
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
    
                cultureList.Add("-- Select Country --");
    
                foreach(CultureInfo culture in cultures)
                {
                    // check if it's not a invariant culture
                    if(culture.LCID != 127)
                    {
                        RegionInfo region = new RegionInfo(culture.LCID);
                        // add countries that are not in the list
                        if(!(cultureList.Contains(region.EnglishName)))
                        {
                            cultureList.Add(region.EnglishName);
                        }
                    }
                }
                cultureList.Sort(); // sort alphabetically
                return cultureList;
            }
    Code I found about this on the internet was all a couple years old and made for windows XP, in 7 you get some extra data. so i found the extra check - if(culture.LCID != 127)

    however now it won't compile on
    Code:
    RegionInfo region = new RegionInfo(culture.LCID);
    giving
    ArgumentException was unhandled
    Culture ID 1 (0x0001) is a neutral culture; a region cannot be created from it.
    Parameter name: culture

    and to be exact this is the 2nd neutral culture it comes across. So why does it get the 1st, and not the 2nd?

    but most important how can I solve this?

  3. #3
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: filling a combobox with countries

    Using Visual Studio 2010 with .Net Framework 4 it compiles for me

  4. #4
    Join Date
    Mar 2012
    Location
    Zwolle, Netherlands
    Posts
    5

    Re: filling a combobox with countries

    wait I have to do a small correction.

    it compiles here as well.

    it is on a form which opens when i press a button on another form.
    when opening the form it crashes.

    I also use MS VS 2010 .NET framework 4.0

  5. #5
    Join Date
    Feb 2012
    Location
    Strasbourg, France
    Posts
    116

    Re: filling a combobox with countries

    Code:
            public List<string> GetCountryList()
            {
                List<string> cultureList = new List<string>();
                CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
    
                cultureList.Add("-- Select Country --");
    
                foreach (CultureInfo culture in cultures)
                {
                    CultureTypes ct = culture.CultureTypes;
                    String s = ct.ToString();
                    if (!s.Contains("NeutralCultures"))
                    {
                        // check if it's not a invariant culture
                        if (culture.LCID != 127)
                        {
                            RegionInfo region = new RegionInfo(culture.LCID);
                            // add countries that are not in the list
                            if (!(cultureList.Contains(region.EnglishName)))
                            {
                                cultureList.Add(region.EnglishName);
                            }
                        }
                    }
                }
                cultureList.Sort(); // sort alphabetically
                return cultureList;
            }
    Added an ugly way to check if it's a Neutral or not :/

  6. #6
    Join Date
    Mar 2012
    Location
    Zwolle, Netherlands
    Posts
    5

    Resolved Re: filling a combobox with countries

    thnx it now works

    since it was an ugly way I like to do those as short as possible.

    Code:
    if (!culture.CultureTypes.ToString().Contains("NeutralCultures"))

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,967

    Re: filling a combobox with countries

    Quote Originally Posted by thef1chesser View Post
    thnx it now works

    since it was an ugly way I like to do those as short as possible.

    Code:
    if (!culture.CultureTypes.ToString().Contains("NeutralCultures"))
    Instead of converting the enum field to a string and then parsing the string, just check if the NeutralCultures flag is present using a bitwise & operator.

    Code:
    if ( (culture.CultureTypes & CultureTypes.NeutralCultures ) != CultureTypes.NeutralCultures )
    {
        ...
    }
    In general, avoid doing operations that convert items into strings whenever possible.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center