-
March 21st, 2012, 10:30 AM
#1
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?
-
March 21st, 2012, 10:58 AM
#2
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?
-
March 22nd, 2012, 03:15 AM
#3
Re: filling a combobox with countries
Using Visual Studio 2010 with .Net Framework 4 it compiles for me
-
March 22nd, 2012, 03:40 AM
#4
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
-
March 22nd, 2012, 04:07 AM
#5
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 :/
-
March 22nd, 2012, 08:11 AM
#6
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"))
-
March 23rd, 2012, 02:01 PM
#7
Re: filling a combobox with countries
Originally Posted by thef1chesser
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|