Is there a way to populate the combo box in winforms with all the country names and another list with the cities of the selected country?

Is there a build procedure in C # to create a list with a list or a list in which there are names of all countries, and when a country is selected, then another list box is filled with cities of this country?

+3
source share
2 answers

Of course, there is a procedure. You can 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 create an instance of this structure, for example. into the ownership 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, create an instance of two Binding Sources (BS):

  • The first BS contacts countries with property.
  • The second BS is bound to the first (DataSource = firstBS), and its DataMember should be "Cities".

:

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

.

+5

. , - , . , , .

0

Source: https://habr.com/ru/post/1704014/


All Articles