Working with combobox


Based on the posts to how to do the state city on the basis of the country selected as in many websites. so here is the example for the country state city list changing dynamically based on the selection of the country and states.

Working with ComboBox in Windows Application.

This is the basic concept which is very useful especially for the people who comes here first time. Generally we see these examples in all the websites and applications.
Here I saw a post regarding the combobox which will bind the data to other combobox dynamically based on the selection of other comboboxes. Here is the Examples for this.

1) Create a new form in the windows form application.
2) Drag and Drop the ComboBox on the form.
Here in my example I taken 3 label controls ( for displaying text on the form(binding text)) and the 3 comboboxes in which it displays the country in the first based on the country it dynamically bind the states , and basing on the selection of the states the cities will bind to the third combobox dynamically.
3) Select the first combobox now the countries are static means not binding dynamically basing on the any other selection so you can bind it by using properties (RAD Model) or using cs code here used items property.
4) Now go to the cs code and then use below code
a) First we need to prepare the states and city lists. So for this you can use string[] or List any one if list is more you can use the generics

List  but here for example I used  string[].
For the cities
string[] ap = { "Anantapur", "Hyderabad", "Kurnool" };
string[] ka = { "Bangalore", "Bellary", "Mangalore" };
string[] tn = { "chennai", "kanchi" };

For the states
string[] india = {"AndhraPradesh","Karnataka","Tamilnadu" };
string[] pakistan = { "Islamabad", "Karachi" };
string[] America = { "NewYork", "Texas" };

b) Next on the change of the country the states has to dynamically bind. So for this we need to find whether the element is changed or not in the combobox for this we need to use Event SelectedIndexChanged. It fires when the index value of the combobox will change.
c) Now select the first combobox where states are binded now goto events of the combobox then select SelectedIndexChanged event. And write below code

private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
{
comboBox2.Items.Clear();
switch (comboBox1.SelectedIndex)
{
case 0: comboBox2.Items.AddRange(india);
break;
case 1: comboBox2.Text=comboBox3.Text="";

comboBox2.Items.AddRange(pakistan);
comboBox3.Items.Clear();
break;
case 2: comboBox2.Text = comboBox3.Text = "";
comboBox2.Items.AddRange(America);
break;
}
}

Here comboBox2.Items.Clear(); is used for clearing the binded items of the combobox2 when every index changed or selection changed.

Here we are using the switch according to the selected item the states will bind. If here index value is 0 we are trying to bind the string[] india to this(means Indian states will bind to this combobox2). For the better understanding comment this and debug it.

comboBox2.Text=comboBox3.Text=""; is used for clearing the text of the comboboxes 2 and 3 incase selection is changed. You can comment this and debug for better understand.
d) Now do same thing for the second combobox.

private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{

comboBox3.Items.Clear();
switch (comboBox2.SelectedIndex)
{
case 0: comboBox3.Text = "";
comboBox3.Items.AddRange(ap);
break;
case 1: comboBox3.Text = "";
comboBox3.Items.AddRange(ka);
break;
case 2: comboBox3.Text = "";
comboBox3.Items.AddRange(tn);
break;
}
}


Here nothing is doing when third combobox is selected. For this you can try the same above process but change the logic to display it int the form or you can do use of this.


Attachments

  • Country-State-City Selection (44114-01337-Country-State-City-Selection.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: