C# ObjectType对象为null,即使向其传递了值
我的表单的SelectIndexchange有问题 例如,我在countryComboBox中添加了国家A和国家B 我现在可以使用countryComboBox的selectIndexChanged选择一个国家。 选择后,我可以添加状态。添加州后,它将显示在countryStateListBox中,然后我可以选择一个州将城镇添加到其中。城镇将显示在城镇列表框中 我遇到的问题是,如果我选择国家B,然后返回国家A,我可以在CountryStates列表框中查看州,但不能在townListBox中查看每个州的城镇。虽然我检查了内存并正确添加了它,但无法在townListbox上正确查看它 基本上,一旦我回到以前添加内容的国家,countryStateListBox\u SelectIndexChanged就会生效 countryStateListBox中的行\u SelectIndexChangedC# ObjectType对象为null,即使向其传递了值,c#,object,null,selectedindexchanged,C#,Object,Null,Selectedindexchanged,我的表单的SelectIndexchange有问题 例如,我在countryComboBox中添加了国家A和国家B 我现在可以使用countryComboBox的selectIndexChanged选择一个国家。 选择后,我可以添加状态。添加州后,它将显示在countryStateListBox中,然后我可以选择一个州将城镇添加到其中。城镇将显示在城镇列表框中 我遇到的问题是,如果我选择国家B,然后返回国家A,我可以在CountryStates列表框中查看州,但不能在townListBox中查看
CountryState state = country.CountryStates.Find(x => x.CountryStateName.Equals(countryStateListBox.SelectedItem));
这就是我的问题所在
比特:
country.CountryStates.Find(x=>x.CountryStateName.Equals(countryStateListBox.SelectedItem))
具有值,但未将其传递给对象“状态”。虽然它以前有用
换到另一个国家。有没有办法解决这个问题
我花了一整天的时间调试这个。
任何帮助都会很好的,泰
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));
}
}
您需要检测是用户触发了列表框上的更改,还是您通过编程触发了更改
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));
}
}
未填充townListBox的问题是因为在您的countryComboBox\u SelectedIndexChanged方法中,有一行用于填充countryStateListBox,而不是townListBox。
在countryComboBox\u SelectedIndexChanged方法中添加一行填充townListBox。我将尝试一下,看看会发生什么。感谢您的回复请记住Peter Trypsteen在其回答中提到的内容,这似乎也与“country.countryStateList.ForEach(x=>countryStateListBox.Items.\u Add(x));”和'triggeredByUser=true;'我添加了
foreach(country.CountryStateList中的var项){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));}
Hi Peter,我实际上尝试过在countryComboBox\u SelectedIndexChanged方法中用另一行代码填充townListBox。当您在ComBoBox中更改country时,它会起作用,因为您可以看到它会更改,但一旦您选择或单击countryStateListBox,townListBox将再次变为空