2016-05-11 3 views
1

Ich möchte meine ComboBox zeigen eine Reihe von Parametern jedes Mal, wenn ich etwas aus der ListBox, aber es zeigt nichts innerhalb der ComboBox.Combobox wird nicht angezeigt Items während der Auswahl des Parameters aus Listbox C#

Das ist, was ich bisher habe ...

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

Hier ist die output

+0

Es sieht so aus, als würden Sie 'comboBox1'-Elemente füllen, wenn die Auswahl von' comboBox1' geändert wird; Sie sollten diesen Code wahrscheinlich in den obigen Event-Handler verschieben ('listBox4_SelectIndexChanged'). Es ist jedoch ein bisschen schwierig zu sagen, wo der Fehler liegt, ohne dass die entsprechende '.Designer.cs' Datei zur Hand ist. – buygrush

Antwort

1

Sie Ihren Code in der falschen Veranstaltung platziert haben.

 // 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 
     } 
0
  1. Sie sollten Ihre Kontrollen sinnvolle Namen geben, damit es einfacher ist, zu bestimmen, was von wo kommt.

  2. Wenn Sie die ComboBox füllen, sollten Sie sie zuerst löschen.

  3. Am wichtigsten ist, dass Sie auf der ComboBox anstelle der ListBox nach dem SelectedIndexChanged suchen. Was passiert, wenn du es hochschiebst?

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) 
{ 
} 
0
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) 
    { 

    } 
+0

Das falsche Ereignis wurde behandelt. Die EreignislisteBox4_SelectedIndexChanged sollte den entsprechenden Code enthalten – vamsi

0

Nun, sollten Sie aktualisieren ( alt entfernen und neue hinzufügen) comboBox1.Items auf listBox4 Änderung:

// 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(); 
    } 
    } 

Und es scheint, dass comboBox1 nützt nichts, zumindest jetzt:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { 
    //TODO: put here logic on comboBox1 change, e.g. on "Retail Management" selection 
} 
Verwandte Themen