[英]ComboBox control shows no items
I'm trying to learn the basics of C# and decided to make a simple windows form to demonstrate the Dictionary class, but when I start my program, the Combo-/ListBox controls stay blank, although I loaded some data to them. 我试图学习C#的基础知识,并决定制作一个简单的Windows窗体来演示Dictionary类,但是当我启动程序时,尽管我向其中加载了一些数据,但Combo- / ListBox控件仍然为空。 Hope you could help me out with this.
希望你能帮我这个忙。
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]);
}
}
}
}
I tried to reproduce your problem but could not. 我试图重现您的问题,但没有。 You need to keep in mind that you have to assign 2 events to your code.
您需要记住,必须为代码分配2个事件。 One on FormLoaded and other on ComboboxSelectionChanged.
一个在FormLoaded上,另一个在ComboboxSelectionChanged上。
I have reduced your code to show universities when selected Index is zero. 当所选索引为零时,我已经减少了您的代码以显示大学。
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]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.