listBox selected item

后端 未结 3 1797
难免孤独
难免孤独 2021-01-23 05:43

I have three listBoxes:

listBox1 have the following items: Fruit and vegetable.

listBox2 have the following items: Orange, Apple, cucumber and Tomato.

li

相关标签:
3条回答
  • 2021-01-23 05:57

    You can try something like this, for easy understanding divide the functionality into functions. I designed this code using win forms however you can apply same code on List boxes as well.

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch(comboBox1.SelectedItem.ToString())
            {
                case "Fruit":
                    FruitSelected();
                    break;
                case "Vegetables":
                    VegetableSelected();
                    break;
                default:
                    NoneSelected();
                    break;
            }
        }
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Similar code as above
        }
        protected void FruitSelected()
        {
            comboBox2.Items.Clear();
            comboBox2.Items.Add("Orange");
            comboBox2.Items.Add("Apple");
        }
        protected void VegetableSelected()
        {
            comboBox2.Items.Clear();
            comboBox2.Items.Add("Tomato");
            comboBox2.Items.Add("Cucumber");
        }
        protected void NoneSelected()
        {
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();
        }
    }
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-23 06:08

    you can try

    listBox1_SelectedIndexChanged(obj ... , sender e)
    {
         if(listBox1.SelectedItem.ToString() == "Fruit")
         {
            listBox2.Items.Add("Orange");
            listBox2.Items.Add("Apple");
          }
         else if()
         {
            // other conditons
          }
    }
    
    listBox2_SelectedIndexChanged(obj ... , sender e)
    {
         if(listBox2.SelectedItem.ToString() == "Apple")
         {
            listBox3.Items.Add("Red");
            listBox3.Items.Add("Green ");
          ........
          }
         else if()
         {
            // other conditons
          }
    }
    

    read http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged.aspx

    0 讨论(0)
  • 2021-01-23 06:21

    You would need to define the OnSelectionChanged Event of the comboBoxes.

    0 讨论(0)
提交回复
热议问题