【发布时间】:2016-01-11 12:17:01
【问题描述】:
在我的代码中,我从一个大的 .xls 文件中获取城市名称和人口数量。我在窗口窗体应用程序的 datagridview 表中设置了这些值。然后我只提取城市名称并将其设置为第一个组合框。第一个组合框项看起来像这样-
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
现在我实现了一个字典来根据第一个组合框更改第二个组合框的值。例如当第一个组合框显示弗伦斯堡时,它会在第二个组合框上显示弗伦斯堡的所有地方。然而,代码是完美的,直到它在主窗体中实现,即 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; }
}
【问题讨论】:
-
(不是您问题的答案)我不会在“数据类”中引用 ComboBox 或 DataGrid,尤其是静态类。总有一天,这段代码会反噬。
标签: c# winforms combobox event-handling selectedindexchanged