I am having trouble with selectIndexChanged of my form.
For example I have country A and country B added to the countryComboBox
I can now select a country with the use of selectIndexChanged of the countryComboBox. Once selected I can add states. Once states are added it will be shown in the countryStateListBox then I can select a state to add town to it. Towns will appear on the townListBox.
The problem I am getting is if I select country B and then come back to the country A, I can view the States in countryStatesListBox but not the towns of each state in townListBox . Although I checked the memory and it was added correctly but cant view it on the townListbox properly.
Basically once Ive returned to the previous country i added stuff with, countryStateListBox_SelectIndexChanged will kick in.
the line in countryStateListBox_SelectIndexChanged
CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));
is where I am having problem with.
The bit :
country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));
has value on it but it is Not being passed to the object "state". Although it worked before changing to another country. Any ideas how to resolve this?
Ive been stuck for a whole day debugging this. Any Help would be great, ty.
private void countryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));
if (country != null)
{
countryStateListBox.Items.Clear();
townListBox.Items.Clear();
country.countryStateList.ForEach(x => countryStateListBox.Items.Add(x));
}
}
private void countryStateListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));
CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));// problem here!
if (state != null)
{
townListBox.Items.Clear();
state.Towns.ForEach(x => townListBox.Items.Add(x));
}
}
You need to detect if it was the user who triggered the change on the ListBoxes or you programatically