C# 使用c根据第一个组合框中的键从文本中获取第二个组合框中的值#
我在代码中创建了两个组合框。第一个是我用来显示德国城市名称的,比如弗伦斯堡、基尔、吕贝克。它以如下方式显示在组合框中C# 使用c根据第一个组合框中的键从文本中获取第二个组合框中的值#,c#,wpf,database,winforms,combobox,C#,Wpf,Database,Winforms,Combobox,我在代码中创建了两个组合框。第一个是我用来显示德国城市名称的,比如弗伦斯堡、基尔、吕贝克。它以如下方式显示在组合框中 Flensburg Kiel Lübeck public Form1() { InitializeComponent(); PlaceList.Combo_list = comboBox1; } 我从.xls文件中得到了这个列表。我使用一个单独的类来获得这个值。我用来获取这个值的代码如下
Flensburg
Kiel
Lübeck
public Form1()
{
InitializeComponent();
PlaceList.Combo_list = comboBox1;
}
我从.xls文件中得到了这个列表。我使用一个单独的类来获得这个值。我用来获取这个值的代码如下
class PlaceList
{
public static ComboBox Combo_list = new ComboBox();
public static DataGridView dataTable = new DataGridView();
public static void List()
{
var startPath = Application.StartupPath;
string folderName = Path.Combine(startPath, "POI_List");
System.IO.Directory.CreateDirectory(folderName);
string SavedfileName = "POI_list.json";
var Saving_path = Path.Combine(folderName, SavedfileName);
string fileName = "Zensus_Gemeinden_org.xlsx";
var path = Path.Combine(startPath, fileName);
String name = "Gemeinden_31.12.2011_Vergleich";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
path + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select [3] as City,[4] as Population, * From [" + name + "$D7:E11300] Where [4] > 10000", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataTable.DataSource = data;
for (int i = 0; i < data.Rows.Count; i++)
{
Combo_list.Items.Add(data.Rows[i]["City"]);
}
string Place_Json = "Place_List:" + JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(Saving_path, Place_Json);
}
}
这一部分对我来说非常有用。现在,下一步是阅读一个文本文件,我在其中为每个地方列表写了几个兴趣点,如下所示
Flensburg,Berlin Wall,Brandenburg Gate,Reichstag Building
Kiel,Nymphenburg palace,Museum Island,Marienplatz
Lübeck,Old Castle,Staatsgalerie Stuttgart,schlossplatz stuttgart
我希望通过单击第一个combobox1来获取此值。假设有人点击Flensburg,他可以看到第二个组合框中的每个值
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
private void LoadKeys()
{
foreach (string line in File.ReadLines("TextFile1.txt"))
{
string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
poi.Add(parts[0], new List<string>(parts.Skip(1)));
}
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
string txt = comboBox1.SelectedItem.ToString();
if (poi.ContainsKey(txt))
{
List<string> points = poi[txt];
comboBox2.Items.Clear();
comboBox2.Items.AddRange(points.ToArray());
}
}
}
Dictionary poi=new Dictionary();
私有void加载密钥()
{
foreach(File.ReadLines(“TextFile1.txt”)中的字符串行)
{
string[]parts=line.Split(新字符[]{',},StringSplitOptions.RemoveEmptyEntries);
poi.Add(零件[0],新列表(零件跳过(1));
}
}
void Combox1\u SelectedIndexChanged(对象发送方,事件参数e)
{
如果(comboBox1.SelectedItem!=null)
{
string txt=comboBox1.SelectedItem.ToString();
if(poi.ContainsKey(txt))
{
列表点=poi[txt];
comboBox2.Items.Clear();
comboBox2.Items.AddRange(points.ToArray());
}
}
}
但它根本不起作用。它不会向组合框2显示任何值。我可以用不同的方式吗。抱歉,描述太长。尝试将断点放入comboBox1\u SelectedIndexChanged,并查看实际发生的情况String txt=Combox1.SelectedItem.ToString();得到了弗伦斯堡,那么if(poi.ContainsKey(txt))也得到了弗伦斯堡。列表点=poi[txt];还有弗伦斯堡。但我不明白为什么它没有在combobox 2中显示任何值检查poi中的内容在我看到的poi中,count=0@volodyadd loadkey到表单加载或在PlaceList之后。Combo\u list=combobox 1在Form1构造函数中