[英]Combobox not showing Items while selecting the parameter from Listbox c#
我希望每次我從 ListBox 中選擇某些內容時,我的 ComboBox 都顯示一組參數,但它沒有在 ComboBox 中顯示任何內容。
這是我到目前為止...
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
這是輸出
您將代碼放在錯誤的事件中。
// This is where your code belongs.
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// THIS WAS THE WRONG PLACE
}
您應該為控件指定有意義的名稱,以便更容易確定來自何處的內容。
每當填充 ComboBox 時,您應該先清除它。
最重要的是,您正在檢查 ComboBox 而不是 ListBox 上的SelectedIndexChanged
。 如果向上移動會發生什么?
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
listBox4.Items.Add("BE");
listBox4.Items.Add("MBA");
listBox4.Items.Add("Pharmacy");
}
private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
if ((string)listBox4.SelectedItem == "BE")
{
comboBox1.Items.Add("CSE");
comboBox1.Items.Add("IT");
comboBox1.Items.Add("ME");
comboBox1.Items.Add("EX");
comboBox1.Items.Add("CE");
}
if ((string)listBox4.SelectedItem == "Pharmacy")
{
comboBox1.Items.Add("Pharmaceutical Chemistry");
comboBox1.Items.Add("Pharmacology");
}
if ((string)listBox4.SelectedItem == "MBA")
{
comboBox1.Items.Add("Retail Management");
comboBox1.Items.Add("HR");
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
好吧,您應該更新(刪除舊的並添加新的) comboBox1.Items
上的listBox4
更改:
// Please, notice "listBox4"
private void listBox4_SelectedIndexChanged(object sender, EventArgs e) {
String selected = listBox4.SelectedItem as String;
// we don't want blinking - too many re-draws
combobox1.BeginUpdate();
try {
//DONE: do not forget to remove old items
combobox1.Items.Clear();
if (selected == "BE") {
combobox1.Items.AddRange("CSE", "IT", "ME", "EX", "CE");
else if (selected == "Pharmacy") {
combobox1.Items.AddRange("Pharmaceutical Chemistry", "Pharmacology");
else if (selected == "MBA")
combobox1.Items.AddRange("Retail Management", "HR");
finally {
combobox1.EndUpdate();
}
}
而且似乎comboBox1
沒有用,至少現在是:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
//TODO: put here logic on comboBox1 change, e.g. on "Retail Management" selection
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.