tags:

views:

669

answers:

2

I wonder if there is already a build procedure in C# for making a combobox or list box that has names of all countries and when a country is selected another combobox is filled with the cities of that country??

Or someone can suggest me what to do!!

A: 

There is no such procedure. I suggest that you make a combo box and populate it with countries, and another one with cities when a country was selected. That way, you have full control over what countries and cities appear in your combo boxes.

cdonner
There is some dll file in windows default folders that have this kinda list (some help on google search) so i wanted to know how i might use those values
Mobin
+2  A: 

Sure there is a procedure. You could start with a simple data structure:

public class Country
{
  public string Name { get; set; }
  public IList<City> Cities { get; set; }

  public Country()
  {
    Cities = new List<City>();
  }
}

public class City { public string Name { get; set; } }

Then, instantiate this structure, e.g. into a property of your form...

Countries =
  new List<Country>
    {
      new Country
        {
          Name = "Germany",
          Cities =
            {
              new City {Name = "Berlin"},
              new City {Name = "Hamburg"}
            }
        },
      new Country
        {
          Name = "England",
          Cities =
            {
              new City {Name = "London"},
              new City {Name = "Birmingham"}
            }
        }
    };

In your form, instantiate two Binding Sources (BS):

  • The first BS binds to the Countries property.
  • The second BS binds to the first (DataSource = firstBS) and its DataMember should be "Cities".

Now you need two dropdowns:

  • 1st: DataSource = first BS, DisplayMember = "Name"
  • 2nd: DataSource = second BS, DisplayMember = "Name"

and you should be pretty much done.

flq
Thanks for the help but i think it involves the manual entry of elements each time the form is loaded and it will be much of a burdonWhat i was looking for was any class in c# in which it is already implemented and we could just bind the values from ther RegardsMubeen
Mobin
Or may be i don't know much about instantiating the structure :-S So can u be bit more expressive about it plzz
Mobin
You have all possibilities of setting up a list of countries with a list of cities each. You could e.g. read it from DB and create the object hierarchy accordingly. Or you can populate it from an XML file. The above example is the quick'n'dirty way of ad-hoc instantiation.
flq