从另一个 class 调用字典到 main class 并将其值设置为组合框
call a dictionary from another class to main class and set its value to combobox
我制作了一个文本文件,其中包含一个城市名称和该城市的许多有趣的地方名称。我希望当城市名称出现在第一个组合框中时,第二个组合框将自动显示所有地名。
为此,在第一步中,我用从大型 .xls 文件中获取的城市名称填充了第一个组合框。然后我用那个城市的城市和地名制作了文本文件。看起来像这样-
Flensburg;Nordertor;Naval Academy Mürwik;Flensburg Firth
Kiel;Laboe Naval Memorial;Zoological Museum of Kiel University
Lübeck;Holstentor;St. Mary's Church, Lübeck;Passat (ship)
我在一个单独的方法中创建字典,现在我想在主窗体中调用这个方法。好吧,我正在以这种方式尝试。但它实际上并没有起作用。
对于数据输入我写了如下代码-
public class POI
{
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
public void poiPlace()
{
foreach (string line in File.ReadLines("POIList.txt"))
{
string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
poi.Add(parts[0], new List<string>());
poi[parts[0]] = new List<string>(parts.Skip(1));
}
}
现在我想在主窗体中调用它
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
POI poi1 =new POI();
poi1.List();
}
public void Combo_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (Combo_list1.SelectedItem != null)
{
string txt = Combo_list1.SelectedItem.ToString();
if (poi.ContainsKey(txt))
{
List<string> points = poi[txt];
Combo_list2.Items.Clear();
Combo_list2.Items.AddRange(points.ToArray());
}
}
}
根本不起作用。
您不要在任何地方调用 poiPlace
以适当地设置 poi
-字典。我想你必须写类似
的东西
POI poi1 = new POI();
poi1.poiList()
而不是
POI poi1 =new POI();
poi1.List();
编辑:您还必须提供一种机制来将数据从字典获取到表单,方法是使字典本身 public
(强烈不推荐)或使用以下内容:
在您的 POI
-class 中添加这两个方法:
public bool ContainsKey(string key) { return this.poi.ContainsKey(key) ; }
public List<string> GetValue(string key) { return this.poi[key]; }
现在可以在您的表单中使用这两种方法:
if (poi1.ContainsKey(txt))
{
List<string> points = poi1.GetValue(txt);
Combo_list2.Items.Clear();
Combo_list2.Items.AddRange(points.ToArray());
}
我制作了一个文本文件,其中包含一个城市名称和该城市的许多有趣的地方名称。我希望当城市名称出现在第一个组合框中时,第二个组合框将自动显示所有地名。
为此,在第一步中,我用从大型 .xls 文件中获取的城市名称填充了第一个组合框。然后我用那个城市的城市和地名制作了文本文件。看起来像这样-
Flensburg;Nordertor;Naval Academy Mürwik;Flensburg Firth
Kiel;Laboe Naval Memorial;Zoological Museum of Kiel University
Lübeck;Holstentor;St. Mary's Church, Lübeck;Passat (ship)
我在一个单独的方法中创建字典,现在我想在主窗体中调用这个方法。好吧,我正在以这种方式尝试。但它实际上并没有起作用。
对于数据输入我写了如下代码-
public class POI
{
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
public void poiPlace()
{
foreach (string line in File.ReadLines("POIList.txt"))
{
string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
poi.Add(parts[0], new List<string>());
poi[parts[0]] = new List<string>(parts.Skip(1));
}
}
现在我想在主窗体中调用它
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
POI poi1 =new POI();
poi1.List();
}
public void Combo_list_SelectedIndexChanged(object sender, EventArgs e)
{
if (Combo_list1.SelectedItem != null)
{
string txt = Combo_list1.SelectedItem.ToString();
if (poi.ContainsKey(txt))
{
List<string> points = poi[txt];
Combo_list2.Items.Clear();
Combo_list2.Items.AddRange(points.ToArray());
}
}
}
根本不起作用。
您不要在任何地方调用 poiPlace
以适当地设置 poi
-字典。我想你必须写类似
POI poi1 = new POI();
poi1.poiList()
而不是
POI poi1 =new POI();
poi1.List();
编辑:您还必须提供一种机制来将数据从字典获取到表单,方法是使字典本身 public
(强烈不推荐)或使用以下内容:
在您的 POI
-class 中添加这两个方法:
public bool ContainsKey(string key) { return this.poi.ContainsKey(key) ; }
public List<string> GetValue(string key) { return this.poi[key]; }
现在可以在您的表单中使用这两种方法:
if (poi1.ContainsKey(txt))
{
List<string> points = poi1.GetValue(txt);
Combo_list2.Items.Clear();
Combo_list2.Items.AddRange(points.ToArray());
}