按下按钮时,我有一个组合框:
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个条目,这对我来说似乎并不过分(有时我在网上看过这个,它提到了大约一千个条目过多)。
答案 0 :(得分:0)
尝试使用ContainsKey
try
{
if (!counties.ContainsKey(items[0)
{
countries.Add(items[0], items[1]);
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}