Arvind
Arvind

Reputation: 11

Combobox not showing Items while selecting the parameter from Listbox c#

I want my ComboBox to show a set of parameters every time I select something from the ListBox, but it is not showing anything inside the ComboBox.

This is what I have so far...

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

Here's the output

Upvotes: 1

Views: 60

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

Well, you should update (remove old and add new) comboBox1.Items on listBox4 change:

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

And it seems that comboBox1 is of no use, at least now:

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

Upvotes: 0

vamsi
vamsi

Reputation: 352

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

    }

Upvotes: 0

Chris Fannin
Chris Fannin

Reputation: 1294

  1. You should give your controls meaningful names so it's easier to determine what is coming from where.

  2. Whenever populating the ComboBox, you should clear it first.

  3. Most importantly, you are checking for the SelectedIndexChanged on the ComboBox instead of the ListBox. What happens if you move it up?

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

Upvotes: 0

C4d
C4d

Reputation: 3292

You've placed your code in the wrong event.

        // 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
        }   

Upvotes: 1

Related Questions