C# 组合框控件不显示任何项

C# 组合框控件不显示任何项,c#,.net,winforms,combobox,listbox,C#,.net,Winforms,Combobox,Listbox,我试图学习C#的基础知识,并决定制作一个简单的windows窗体来演示Dictionary类,但是当我启动程序时,Combo-/ListBox控件保持空白,尽管我向它们加载了一些数据。希望你能帮我解决这个问题 using System; using System.Collections.Generic; using System.Windows.Forms; namespace ClassDictionaryExample { public partial class Form1 :

我试图学习C#的基础知识,并决定制作一个简单的windows窗体来演示Dictionary类,但是当我启动程序时,Combo-/ListBox控件保持空白,尽管我向它们加载了一些数据。希望你能帮我解决这个问题

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace ClassDictionaryExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Dictionary<string, string[]> CountryList = new Dictionary<string, string[]>();

        private void Form1_Load(object sender, EventArgs e)
        {
            CountryList["Bulgaria"] = new string[] { "Sofia University St Kliment Ohridski", "Technical University of Sofia", 
                "Plovdiv University Paisii Hilendarski" };
            CountryList["Romania"] = new string[] { "Alexandru Ioan Cuza University", "Babes-Bolyai University", 
                "University of Bucharest" };
            CountryList["Serbia"] = new string[] { "University of Belgrade", "University of Novi Sad", "University of Niš" };

            foreach (var CountryKey in CountryList.Keys)
            {
                comboBoxCountry.Items.Add(CountryKey);
            }

            comboBoxCountry.SelectedIndex = 0;
        }

        private void comboBoxCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedCountry = comboBoxCountry.SelectedItem.ToString();

            if (comboBoxCountry.SelectedIndex == 0)
                listBoxUniversities.Items.Clear();
            else 
            {
                listBoxUniversities.Items.Clear();
                listBoxUniversities.Items.AddRange(CountryList[selectedCountry]);
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
名称空间类字典示例
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
字典国家列表=新字典();
私有void Form1\u加载(对象发送方、事件参数e)
{
国家列表[保加利亚] =新字符串[] {索非亚大学圣克利姆特奥德里奇,“索非亚技术大学”,
“普洛夫迪夫大学Paisii Hilendarski”};
CountryList[“Romania”]=新字符串[]{“Alexandru Ioan Cuza University”,“Babes Bolyai University”,
“布加勒斯特大学”};
国家名单[“塞尔维亚”]=新字符串[]{“贝尔格莱德大学”、“诺维萨德大学”、“尼什大学”};
foreach(CountryList.Keys中的var CountryKey)
{
comboBoxCountry.Items.Add(CountryKey);
}
comboBoxCountry.SelectedIndex=0;
}
private void ComboxCountry\u SelectedIndexChanged(对象发送方,事件参数e)
{
string selectedCountry=comboBoxCountry.SelectedItem.ToString();
如果(comboBoxCountry.SelectedIndex==0)
listBoxUniversities.Items.Clear();
其他的
{
listBoxUniversities.Items.Clear();
listboxuniversions.Items.AddRange(CountryList[selectedCountry]);
}
}
}
}

我试图重现您的问题,但无法重现。您需要记住,您必须为代码分配两个事件。一个在FormLoaded上,另一个在ComboboxSelectionChanged上

我减少了你的代码,以便在所选索引为零时显示大学

public Form1()
{
    InitializeComponent();

    this.Load += Form1_Load;
    comboBoxCountry.SelectedIndexChanged += comboBoxCountry_SelectedIndexChanged;
}

private Dictionary<string, string[]> _countryList;
public Dictionary<string, string[]> CountryList
{
    get
    {
        if (_countryList == null)
        {
            _countryList = new Dictionary<string, string[]>();
            _countryList["Bulgaria"] = new string[] { "Sofia University St Kliment Ohridski", "Technical University of Sofia", "Plovdiv University Paisii Hilendarski" };
            _countryList["Romania"] = new string[] { "Alexandru Ioan Cuza University", "Babes-Bolyai University", "University of Bucharest" };
            _countryList["Serbia"] = new string[] { "University of Belgrade", "University of Novi Sad", "University of Niš" };
        }

        return _countryList;
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    foreach (var CountryKey in CountryList.Keys)
        comboBoxCountry.Items.Add(CountryKey);

    comboBoxCountry.SelectedIndex = 0;
}

private void comboBoxCountry_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedCountry = comboBoxCountry.SelectedItem.ToString();

    listBoxUniversities.Items.Clear();
    listBoxUniversities.Items.AddRange(CountryList[selectedCountry]);
}
public Form1()
{
初始化组件();
此.Load+=表格1\U Load;
comboBoxCountry.SelectedIndexChanged+=ComboxCountry\u SelectedIndexChanged;
}
私人字典(u countryList),;
公共词典国家列表
{
收到
{
如果(_countryList==null)
{
_countryList=新字典();
[保加利亚] =新弦[] {索非亚大学圣克利姆特-奥里德基”、“索非亚技术大学”、“普罗夫迪夫大学Pasii-Heldalkys}”;
_countryList[“Romania”]=新字符串[]{“Alexandru Ioan Cuza大学”、“Babes Bolyai大学”、“布加勒斯特大学”};
_国家名单[“塞尔维亚”]=新字符串[]{“贝尔格莱德大学”、“诺维萨德大学”、“尼什大学”};
}
返回(u countryList);;
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
foreach(CountryList.Keys中的var CountryKey)
comboBoxCountry.Items.Add(CountryKey);
comboBoxCountry.SelectedIndex=0;
}
private void ComboxCountry\u SelectedIndexChanged(对象发送方,事件参数e)
{
string selectedCountry=comboBoxCountry.SelectedItem.ToString();
listBoxUniversities.Items.Clear();
listboxuniversions.Items.AddRange(CountryList[selectedCountry]);
}

Yes@GrantWinney是对的。我也这样做了。但是,您需要记住,必须为代码分配2个事件。一个在formloaded上,另一个在combobox选择上更改。谢谢。我忘记分配“Form1\u Load”事件。现在修好了。