【问题标题】:Selectedindex change for combobox is not working using c#组合框的 Selectedindex 更改无法使用 c#
【发布时间】: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


【解决方案1】:

查看您的代码,我看不到您将回调分配给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 移动到表单内,将它们留在数据类中是不安全的。

【讨论】:

  • 对不起,我无法理解您的回答。我的意思是在哪里可以添加 Combo_list1.SelectedIndexChanged += Combo_list_SelectedIndexChanged;此外,如果我将代码设为静态,则字典无法正常工作。
  • 没关系,但为什么你将 ComboBox 设为静态?我会修正我的答案。
  • 谢谢。我很新处理这种情况。你能告诉我我应该怎么做才能让它完美地工作。
  • 我修改了答案,这应该可以解决您的组合行为!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 2017-07-18
  • 2014-03-30
  • 1970-01-01
相关资源
最近更新 更多