Hey all I am currently practicing my error handling processes and am having trouble filling up a combobox with a list of all the countries so that the user can choose what country they are located in. What I am creating is something like a registration form. I found an algorithm in the internet about filling up the combobox, but I do not understand it and it does not work properly in windows 7, it gets a There is no region associated with the invariant culture sort of error and does not run properly, it compiles but does not run. Here is the algorithm:

private void RegisterUser_Load(object sender, EventArgs e)
        {
            comBoxCountry.Items.Insert(0, "-- Select Country --");
            comBoxCountry.SelectedIndex = 0;

            comBoxEmployeeNum.Items.Insert(0, "-- Select Number of Employees --");
            comBoxEmployeeNum.SelectedIndex = 0;

            comBoxReqType.Items.Insert(0, "-- Select Request Type --");
            comBoxReqType.SelectedIndex = 0;
            foreach (string country in getCountryList())
            {
                comBoxCountry.Items.Add(country);
            }

            for (int i = 1; i < 51; i++)
            {
                comBoxEmployeeNum.Items.Add(Convert.ToString(i));
            }

            for (int i = 1; i < 11; i++)
            {
                comBoxReqType.Items.Add("Request " + Convert.ToString(i));
            }
        }

        public static List<string> getCountryList()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

            foreach (CultureInfo culture in cultures)
            {
                RegionInfo region = new RegionInfo(culture.LCID);
                //RegionInfo region = new RegionInfo(culture.LCID);

                if (!(cultureList.Contains(region.EnglishName)))
                {
                    cultureList.Add(region.EnglishName);
                }
            }
            return cultureList;
        }

It gets the error at this line specifically:

RegionInfo region = new RegionInfo(culture.LCID);

I tried running this algorithm in windows xp and it works flawlessly. I don't know what's wrong here. I also do not understand the algorithm :( If anyone could show or give hints on how I can create something similar I would be very grateful!

Oh and what is the difference between keypress and keydown? as I understand keypress is a combination of keydown and keyup, but that's only thing I understand about it. I am thinking of using it in my error handling processes.

Thanks :D

The CultureInfo class holds culture-specific information, such as the associated language, sublanguage, country/region, calendar, and cultural conventions.

This just creates an array of cultureInfo classes by asking the system for all that it knows about, then attempts to extract a string from that object representing the culture's country.

The line where you get the error is where you are creating a regionInfo object that accepts a culture Identifier. The error you get is that one of the culture's in windows 7 allows the "invariant culture" object to show up when you ask for all cultures this way. and "invariant culture" represents an empty culture object with the ID 0x007F (or 127)

so we just check for that and it will work. just replace your getCountryList method to this one and it will all work fine.

public static List<string> getCountryList()
        {
            List<string> cultureList = new List<string>();
            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

            foreach (CultureInfo culture in cultures)
            {
                if (culture.LCID != 127)
                {
                    RegionInfo region = new RegionInfo(culture.LCID);
                    //RegionInfo region = new RegionInfo(culture.LCID);

                    if (!(cultureList.Contains(region.EnglishName)))
                    {
                        cultureList.Add(region.EnglishName);
                    }
                }
            }

            cultureList.Sort(); //put the country list in alphabetic order.

            return cultureList;
        }

As for the KeyPressed and KeyDown events. Key pressed is when "some" keys are both depressed and released. and KeyDown is when any key is pressed down. but fires before its released. There are tons of articles out there about it. If you want to know more about using them in a error handling fashion ddanbe has posted a good snippet here at daniweb. here is the link.

Thank you so much for this Diamonddrake, I was certain that it had something to do with windows 7. Thank you for your input! Much appreciated!

Also, thank you for the reading material on error handling with keypress and keydown, I shall read it now!

Thanks! I will just mark this as solved then!

Glad I could be of some help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.