省市连接代码问题
两个listbos,第一个是省(Province),第二个是市(City),代码如下,我运行的时候为什么在第一个listbox中选定了一个省,第二个里面没任何东西?
private void Form1_Load(object sender, EventArgs e)
{
Province.Items.Add("湖南省");
Province.Items.Add("江苏省");
Province.SelectedIndex = 0;
}
private void City_SelectedIndexChanged(object sender, EventArgs e)
{
if (Province.SelectedItem != null)
{
string[] hncity = { "长沙市", "株洲市", "湘潭市" };
string[] jscity = { "常州市", "淮安市", "连云港市" };
string slstr = Province.SelectedItem.ToString().Trim();
City.Items.Clear();
switch (slstr) {
case "湖南省":
City.Items.AddRange(hncity);
break;
case "江苏省":
City.Items.AddRange(jscity);
break;
}
}
}
------解决方案--------------------写在Province的SelectedIndexChanged中才行。
------解决方案--------------------private void City_SelectedIndexChanged(object sender, EventArgs e)
是省的那个listbox的SelectedIndexChanged事件,应该是Province_SelectedIndexChanged(object sender, EventArgs e)