即使正在向其传递值,ObjectType对象也为null

时间:2013-09-02 17:07:10

标签: c# object null selectedindexchanged

我的表单的selectIndexChanged出现问题。

例如,我将country A和country 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));

有值,但它没有传递给对象“state”。虽然它以前工作过 换到另一个国家。有任何想法如何解决这个问题?

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

2 个答案:

答案 0 :(得分: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));
    }
}

答案 1 :(得分:0)

你没有填充townListBox的问题是因为在你的countryComboBox_SelectedIndexChanged方法中,你有一行填充countryStateListBox而不是townListBox。 添加一行以在countryComboBox_SelectedIndexChanged方法中填充townListBox。