在我的代码中,我从一个大的.xls文件中获取城市名称和人口编号。我在窗口表单应用程序的datagridview表中设置这些值。然后我只提取城市名称并将其设置为第一个Combobox。 fisrt组合框项目看起来像这样 -
Flensburg
Kiel
Lübeck
然后我制作一个文本文件,其中包含该城市的名称和有趣的地方列表。我的txt文件看起来像这样。
Flensburg;Nordertor;Naval Academy Mürwik;Flensburg Firth
Kiel;Laboe Naval Memorial;Zoological Museum of Kiel University;Kieler Förde;German submarine U-995;Sparkassen-Arena;Aquarium GEOMAR;Old Botanical Garden, Kiel;Holstein-Stadion;Botanischer Garten der Christian-Albrechts-Universität zu Kiel;Schwentine;Schulensee;Lanker See;Postsee;Rosensee
Lübeck;Holstentor;St. Mary's Church, Lübeck;Passat (ship);Burgtor;Lübeck Museum of Theatre Puppets;Trave;St. Catherine's Church, Lübeck;Lübeck Cathedral;St. Anne's Museum Quarter, Lübeck;Behnhaus;Behnhaus;Theater Lübeck;Ratzeburger See;Bucu;Hemmelsdorfer See;Stadion an der Lohmühle;Dassower See
现在我实现了一个字典来根据第一个组合框更改第二个组合框值。例如当第一个组合框展示弗伦斯堡时,它将在第二个组合框上显示弗伦斯堡的所有地方。然而,代码是完美的,直到它在主Form中实现,即Form1.cs。但是现在我尝试用另一个类用单独的方法编写它。所以现在我面临问题,因为我无法修复objectsender和evetaverage问题并将其传递给Form1.cs。
我的数据类代码是 -
namespace POIList
{
public class POI
{
public static ComboBox Combo_list1 = new ComboBox();
public static ComboBox Combo_list2 = new ComboBox();
public static DataGridView dataTable = new DataGridView();
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();
public 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_list1.Items.Add(data.Rows[i]["City"]);
}
string Place_Json = "Place_List:" + JsonConvert.SerializeObject(data, Formatting.Indented);
File.WriteAllText(Saving_path, Place_Json);
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 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());
}
}
}
}
}
Form1.cs是
namespace TouristPlace
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
POI.Combo_list1 = comboBox1;
POI.dataTable = dataGridView1;
POI poi1 =new POI();
poi1.List();
POI poi2 = new POI();
//poi2.Combo_list_SelectedIndexChanged();
}
private void button1_Click(object sender, EventArgs e)
{
}
public string ComboText1
{
get { return comboBox1.Text; }
set { comboBox1.Text = value; }
}
public string ComboText2
{
get { return comboBox2.Text; }
set { comboBox2.Text = value; }
}
答案 0 :(得分:0)
查看您的代码,我无法看到您将回调分配给Combo_list1.SelectedIndexChanged
事件的位置。也许这就是问题所在。
尝试添加:
Combo_list1.SelectedIndexChanged += Combo_list_SelectedIndexChanged;
在POI类中的,例如在List()函数中。确保仅调用List()函数一次,否则将多次调用回调函数。如果你不能确定,你可以这样做:
Combo_list1.SelectedIndexChanged -= Combo_list_SelectedIndexChanged;
Combo_list1.SelectedIndexChanged += Combo_list_SelectedIndexChanged;
无论如何,正如其他人在OP中告诉你的那样,你应该将ComboBox移到表单中,将它们留在数据类中是不安全的。