【问题标题】:Winforms combobox has lag populatingWinforms 组合框填充滞后
【发布时间】:2012-12-25 19:50:29
【问题描述】:

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

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 个条目,这对我来说似乎并不过分(其他时候我在网上看到过这个讨论,它提到一千左右的条目过多)。

【问题讨论】:

  • 看看这一行你的括号怎么不对齐 if(items[2] !=string.Empty && items[3] !=string.Empty) stateCodes.Add(items [2],项目[3]); }
  • 看起来您正在阅读该文件中的所有行。文件有多少行?磁盘上文件的大小是多少?
  • 正在读取的文件大约有 350 行
  • 此外,如果这是可以一次又一次使用的数据,为什么不将值存储在数据库中并将状态代码绑定到下拉或使用 web 服务的代码..
  • 输入文件中有多少行导致添加到countries时抛出异常?

标签: c# winforms combobox


【解决方案1】:

尝试使用ContainsKey

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

        }

【讨论】:

  • 我需要用 制作字典。由于字典中不能有重复的键,因此这是我能想到的“作弊”的唯一方法——读取包含多个键的数据。
  • 嗯...仅包含第一个国家/地区。再次编辑:好的,这确实有效!我没有一路通过调试器!谢谢!
猜你喜欢
  • 2012-02-15
  • 2016-05-21
  • 2013-06-22
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多