【问题标题】:ObjectType Object is null even a value is being passed to itObjectType 对象为空,即使有值被传递给它
【发布时间】:2013-09-05 20:12:48
【问题描述】:

我的表单的 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));
        }
    }

【问题讨论】:

    标签: c# object null selectedindexchanged


    【解决方案1】:

    您需要检测是用户触发了 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));
        }
    }
    

    【讨论】:

    • 我会试试这个,看看会发生什么。感谢回复
    • 记住 Peter Trypsteen 在他的回答中提到的似乎也很相关的内容
    • 'country.countryStateList.ForEach(x => countryStateListBox.Items._Add_(x));'和
    • 'triggeredByUser = true ;'
    • 我已添加foreach(var item in country.CountryStateList) { CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(item.ToString()); if(state != null) { townListBox.Items.Clear(); state.Towns.ForEach(x => townListBox.Items.Add(x)); }
    【解决方案2】:

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

    【讨论】:

    • 嗨彼得,我实际上尝试用 countryComboBox_SelectedIndexChanged 方法中的另一行代码填充 townListBox。当您在 ComBoBox 中更改国家/地区时,它会起作用,因为您可以看到它发生了变化,但是一旦您选择或单击 countryStateListBox,townLisBox 再次变为空
    猜你喜欢
    • 2018-11-24
    • 2017-01-02
    • 1970-01-01
    • 2010-12-15
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多