0

我的表单的 selectIndexChanged 有问题。

例如,我将国家 A 和国家 B 添加到 countryComboBox

我现在可以使用 countryComboBox 的 selectIndexChanged 来选择一个国家。选择后,我可以添加状态。一旦添加了州,它将显示在 countryStateListBox 中,然后我可以选择一个州来添加城镇。城镇将出现在 townListBox 上。

我遇到的问题是,如果我选择 B 国然后回到 A 国,我可以在 countryStatesListBox 中查看州,但在 townListBox 中看不到每个州的城镇。虽然我检查了内存并且它被正确添加但无法在 townListbox 上正确查看。

基本上,一旦我回到我添加的前一个国家,countryStateListBox_SelectIndexChanged 就会启动。

countryStateListBox_SelectIndexChanged 中的行

CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

是我遇到问题的地方。

位:

country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

有它的价值,但它没有被传递给对象“状态”。虽然它在改变到另一个国家之前有效。任何想法如何解决这个问题?

我已经被困了一整天调试这个。任何帮助都会很棒,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));
        }
    }
4

2 回答 2

0

您没有填充 townListBox 的问题是因为在您的 countryComboBox_SelectedIndexChanged 方法中,您有一行用于填充 countryStateListBox 但没有填充 townListBox。在 countryComboBox_SelectedIndexChanged 方法中添加一行用于填充 townListBox。

于 2013-09-02T17:46:39.897 回答
0

您需要检测是用户触发了 ListBoxes 上的更改还是您以编程方式

private bool triggeredByUser = true ;

private void countryComboBox_SelectedIndexChanged(object sender, EventArgs e)
{           
    Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));

    if (country != null)
    {
        triggeredByUser = false ;

        countryStateListBox.Items.Clear();
        //townListBox.Items.Clear();
        country.countryStateList.ForEach(x => countryStateListBox.Items.Add(x));

        //by default select the first state
        countryStateListBox.selectedIndex = 0 ;
        CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

        //refresh the town list based on the country and state
        refreshTownList(country,state) ;

        triggeredByUser = true ;
    }
}




private void countryStateListBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if(triggeredByUser)
    {
        Country country = Countries.Find(x => x.CountryName.Equals(countryComboBox.SelectedItem));
        CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));

        //refresh the town list based on the country and state
        refreshTownList(country,state) ;
    }
}

private refreshTownList(Country country,CountryState state)
{
    if (state != null)
    {
        townListBox.Items.Clear();
        state.Towns.ForEach(x => townListBox.Items.Add(x));
    }
}
于 2013-09-02T17:21:56.640 回答