0

当您按下按钮时,我有一个组合框:

private void button1_Click(object sender, EventArgs e)
    {

        Dictionary<string, string> countries = new Dictionary<string, string>();
        //Dictionary<string, List<State>> stateList=new Dictionary<string,List<State>>();
        HashSet<string> country = new HashSet<string>();

        var lines = File.ReadAllLines("CountryState.txt");

        foreach (string line in lines)
        {
            string[] items = line.Split('\t').ToArray();

            try
            { 
                countries.Add(items[0], items[1]);
            }
            catch (Exception ex)
            {
                  //MessageBox.Show(ex.Message);

            }

            country.Add(items[0]);

            if (!stateList.ContainsKey(items[0])) {
               stateList.Add(items[0], new List<State>());
            }

            if (items[2] != "")
            {
               stateList[items[0]].Add(new State(items[2], items[3]));
            }
            if(items[2] !=string.Empty && items[3] !=string.Empty)
                stateCodes.Add(items[2], items[3]);

            }
            comboBox1.DataSource = countries.Keys.ToList();
    }

comboBox1 数据源最多有 234 个条目,这对我来说似乎并不过分(其他时候我在网上看到过这个讨论,它提到一千左右的条目过多)。

4

1 回答 1

0

尝试使用ContainsKey

        try
        {
           if (!counties.ContainsKey(items[0)
           {
             countries.Add(items[0], items[1]);
           }
        }
        catch (Exception ex)
        {
              //MessageBox.Show(ex.Message);

        }
于 2012-12-25T23:29:14.127 回答