如何避免组合框中的串联

本文关键字:何避免 组合 | 更新日期: 2023-09-27 18:27:34

我正在尝试设计一个简单的windows窗体应用程序。如果我选择一个代表国家的组合框。再说一遍,如果我选择任何一个国家,那么在另一个组合框中,该国家的州就会出现。最后,如果我们选择一个州,那么相应的城市将出现在另一个组合框中。如果我第一次做手术,效果很好。。但第二次,如果我要做任何修改,那么它是串联的。。这是我的代码-

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedItem.ToString() == "INDIA")
    {
        string[] Country = { "Andhra Pradesh", 
       "Karnataka","Maharashtra"};
        comboBox2.Items.AddRange(Country);            
    }
    else if (comboBox1.SelectedItem.ToString() == "USA")
    {
        comboBox2.Items.AddRange(new string[] { "Alabama", 
        "Alaska", "Arizona"});
    }
    else if (comboBox1.SelectedItem.ToString() == "CHAINA")
    {
        comboBox2.Items.AddRange(new string[] { "Beijing Municipality", 
        "Tianjin Municipality", "Hebei Province"});
    }        
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{        
    if (comboBox2.SelectedItem.ToString() == "Andhra Pradesh")
    {
        comboBox3.Items.AddRange(new string[] { "Adilabad","Anantapur","Chittoor","Kakinada","Guntur","Hyderabad",            "Khammam","Kurnool","Medak","Nizamabad","Visakhapatnam","Warangal","Eluru"}); 
    }
    else if (comboBox2.SelectedItem.ToString() == "Karnataka")
    {
        comboBox3.Items.AddRange(new string[] { "Bengaluru Urban", "Bengaluru Rural", "Chikkaballapur", "Kolar", "Mysuru" });
    }
    else if (comboBox2.SelectedItem.ToString() == "Maharashtra")
    {
        comboBox3.Items.AddRange(new string[] { "Mumbai", "Pune", "Nagpur", "Thane", "Nashik" });
    }
    else if (comboBox2.SelectedItem.ToString() == "Alabama")
    {
        comboBox3.Items.AddRange(new string[] { "Margaret","Helena","Southside","Mountain Brook","Vestavia Hills"});
    }
    else if (comboBox2.SelectedItem.ToString() == "Alaska")
    {
        comboBox3.Items.AddRange(new string[] { "Anchorage", "Fairbanks", "Juneau", "Sitka", "Ketchikan" });
    }
    else if (comboBox2.SelectedItem.ToString() == "Arizona")
    {
        comboBox3.Items.AddRange(new string[] { "Apache Junction", "Avondale", "Benson", "Buckeye", "Bullhead City" });
    }
    else if (comboBox2.SelectedItem.ToString() == "Beijing Municipality")
    {
        comboBox3.Items.AddRange(new string[] { "Dongcheng", "Xicheng", "Chaoyang", "Fengtai ", "Miyun " });
    }
    else if (comboBox2.SelectedItem.ToString() == "Tianjin Municipality")
    {
        comboBox3.Items.AddRange(new string[] { "Heping", "Hedong", "Xiqing", "Nankai", "Beichen" });
    }
    else if (comboBox2.SelectedItem.ToString() == "Hebei Province")
    {
        comboBox3.Items.AddRange(new string[] { "Baoding‎", "Bazhou", "Chengde‎", "Handan‎", "Langfang‎" });
    }        
}

请建议我在代码中应该修改什么?

如何避免组合框中的串联

由于每次从combobox1(国家)或combobox2(州)中选择要"重新开始"的内容时,必须首先清除相应事件上的次要combobox,例如:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    combobox2.Items.Clear();
    combobox3.Items.Clear();
    . . .
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
    combobox3.Items.Clear();
    . . .
}

此外,最好为控件提供有意义的名称。与其让他们保留默认的通用名称,不如给他们命名,如comboboxCountrys、comboboxStates和comboBoxCities。这将使调试更加容易。

最后,最好将地点列表(国家、州和省、城市/城镇)保存在数据库中,或者至少保存在某种单独维护的列表中。随意地把它们都放在迷宫中,如果方块最终会让你斗鸡眼。