0

I have three dependant checkboxlists. 1. Countries 2. States 3. Cities I want to list all the States if the particular Country is selected in the Countries checkboxlist. And similarly if i select any State then the respective Cities should be populated in the cities checkboxlist.

I have created separate functions for States for every Country and calling them with the following code:

private void Country_SelectedIndexChanged(object sender, EventArgs e)
        {

            foreach (string s in Country.CheckedItems)
            {
                if (Country.CheckedItems.Contains("US"))
                {
                    US_States_Checked();
                }

                if (Country.CheckedItems.Contains("Canada"))
                {
                    Canada_States_Checked();
                }

            }


        }

public void Canada_States_Checked()
        {
            string[] canada_states = new string[12];
            canada_states[0] = "Alberta";
            canada_states[1] = "British Columbia";
            canada_states[2] = "Manitoba";
            canada_states[3] = "New Brunswick";
            canada_states[4] = "Newfoundland and Labrador";
            canada_states[5] = "Northwest Territories";
            canada_states[6] = "Nova Scotia";
            canada_states[7] = "Ontario";
            canada_states[8] = "Prince Edward Island";
            canada_states[9] = "Quebec";
            canada_states[10] = "Saskatchewan";
            canada_states[11] = "Yukon Territory";

            State.Items.AddRange(canada_states);
        }

I have the following problems: 1. Which property is used for detecting when the checkbox is UnChecked? 2. How to make a check on the name of selected state/country and check whether it is Checked or Not? Something like:

if(country.selectedItem.Equals("US") and country.selectedItem is unchecked....)) {
.......
} 
  1. How to remove/clear the particular states/cities when the country is unchecked keeping in mind that it shouldn't remove the states of any other country listed in the states checkboxlist?

Problem is simple but a bit tricky.

Thanks

  • first is it for Winform or WPF ? then your best solution will be to use an object that contain the country and the state/province. this will allow you to remove from the state list all states that their country value is the one you unchecked. – Franck May 07 '15 at 11:18
  • I am using Windows Form. I still could not figure it out how to check if a checkbox inside checkboxlist is unchecked? Can you provide any link as an example? Thanks – Nabeeha Deedar May 07 '15 at 13:00

1 Answers1

0

Separate function for each country is pretty bad idea. You should store data outside of your code. Consider using XML or database or JSON to store data. You can read about XML serialization here and here, for example

After loading data from file to memory you can place it in the dictionary like

Dictionary<string, Dictionary<string, List<String>>> data;
//access data   
var cities = data["Canada"]["Ontario"];

And final part - pass selected item to your dictionary:

var states = data[country.selectedItem].Keys;

and

var cities = data[country.selectedItem][state.selectedItem];

I suppose that country and state are names of your CheckBoxList objects.

As for CheckBoxList usage - just implement your logic, all needed methods are described here. (I am still not sure, what are you using: WPF or WinForms or what, but you can find proper docs).

SelectedIndices and SelectedItems return selected elements of checkBoxList.

You can check like this:

var selectedItems = country.SelectedItems;
if (selectedItems.Contains("Canada")
{
//Do what you need
}
Community
  • 1
  • 1
Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84
  • Thanks for the idea. Yes country, state and city are the names of my checkboxlists. I am using Windows Form. Still couldn't figure out the property / method to detect if an item inside a checkedListbox is unchecked and extract its value as well for further comparison? – Nabeeha Deedar May 07 '15 at 14:35