2015-12-15 54 views
-1

我的窗口中有两个Combobox窗体application.1st Combobox显示我已经从xls文件从数据库获得的国家的城市名称。组合框显示城市名这样的 -如何从文本文件中获取密钥和值并在组合框中设置值

 Berlin 
    Munich 
    Stuttgart //etc. 

现在我写了包含该地区的兴趣点列表的文本文件。我的文本文件看起来像

Berlin,Berlin Wall,Brandenburg Gate,Reichstag Building 
Munich,Nymphenburg palace,Museum Island,Marienplatz 
Stuttgart,Old Castle,Staatsgalerie Stuttgart,schlossplatz stuttgart //etc 

现在我想,当一个地方出现在第一个组合框,所有的POI项目将会自动以列表的形式一样,当我点击柏林第二次组合框第二个下拉框生成将显示

Berlin Wall 
    Brandenburg Gate 
    Reichstag Building 

但我不确定如何继续这样做。

我的代码是 - 填写follwing方式

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); 

     } 
    } 
    } 

然后我把它从From1.cs像

组合框1这

public partial class Form1 : Form 
{ 
    public Form1() 
    { 

     InitializeComponent(); 
     PlaceList.Combo_list = comboBox1; 


    } 
} 
+0

您需要通过线加载您的文件和行建是词典中的条目,其中关键是具有城市名称的字符串,值是a POI字符串列表。然后,当你从组合中选择一个城市时,只需使用该字符串作为搜索字典的关键 – Steve

+0

谢谢但是,我很新,处理这个问题,你有任何这样的例子。 @Steve – Nowshin

+1

你为什么要拷贝部分@Steve的问题答案? –

回答

1

您需要加载您的文件和行按行在字典中构建一个条目,其中键是具有城市名称的字符串,值是具有POI的字符串列表。然后,当你从下拉列表中选择一个城市只需要使用字符串作为关键字来搜索你的字典

例如,这是一个完整的示例,你可以测试使用LinqPAD,并告诉你如何能读你的txt文件,并建立一个,让您的兴趣点按城市名称键入全局变量

Form f; 
ComboBox cboCities; 
ComboBox cboPoi; 
Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>(); 
void Main() 
{ 
    f = new Form(); 
    cboCities = new ComboBox(); 
    cboCities.DropDownStyle = ComboBoxStyle.DropDownList; 
    cboCities.Items.AddRange(new string[] { "Berlin", "Munich", "Stuttgart"}); 
    cboCities.SelectedIndexChanged += cboCities_SelectedIndexChanged; 
    f.Controls.Add(cboCities); 
    cboPoi = new ComboBox(); 
    cboPoi.Location = new System.Drawing.Point(0, 30); 
    f.Controls.Add(cboPoi); 
    foreach (string line in File.ReadLines(@"D:\temp\poi.txt")) 
    { 
     string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 
     poi.Add(parts[0], new List<string>(parts.Skip(1))); 
    } 
    f.ShowDialog(); 
} 
void cboCities_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string txt = cboCities.SelectedItem.ToString(); 
    if (poi.ContainsKey(txt)) 
    { 
     List<string> points = poi[txt]; 
     cboPoi.Items.Clear(); 
     cboPoi.Text = string.Empty; 
     cboPoi.Items.AddRange(points.ToArray()); 
    } 
} 
+0

嗨,这是行不通的。我试了几次。最初只有一次它的作品,它停止显示任何价值。 @Steve – Nowshin

+0

奇怪的是,我刚刚测试过一个简单的应用程序,并按预期工作。你能否在事件处理程序中放置一个断点,并检查每次更改城市组合内容时是否正确调用了代码? – Steve

+0

是的。我检查。文本值显示为空 – Nowshin

0

这是我的编辑答案:

private void button1_Click(object sender, EventArgs e) 
    { 

     LoadKeys(); 

    } 
    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>()); 
        poi[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()); 
      } 
     } 
    } 
相关问题