Tuesday, July 22, 2014

C# - ComboBox with all countries

I suppose two posts won't hurt anyone, this one will be short don't worry. 

I am learnong C# on the side, and as a learning project  I am currently developing a program to help our Service desk automate Account creations and modifications. One of the fields that I want them to fill out when creating accounts is the Country where the user is located, but I honestly didn't want to trust their typing skills on this so I though I could try to create a drop down menu - or ComboBox as its called in the Toolbox - and I figured I should look online maybe there is a text list that I can find already made. 

It turns out there is a whole reference to get the list that you can use for this. Here is the Sauce where I found the information, and I'll put the information below in case you are looking for a quick answer:

Add reference
using System.Globalization;
Country combobox without list of country with NativeName and so more.
Code :
private void PopulateCountryComboBox()
            RegionInfo country = new RegionInfo(new CultureInfo("en-US", false).LCID);
            List countryNames = new List();
            foreach (CultureInfo cul in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
                country = new RegionInfo(new CultureInfo(cul.Name, false).LCID);

            IEnumerable nameAdded = countryNames.OrderBy(names => names).Distinct();

            foreach (string item in nameAdded)

Country dropdown HTML