从列表框c#

时间:2016-05-11 15:56:13

标签: c# winforms

我希望每次从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");
        }
    }

这是output

4 个答案:

答案 0 :(得分:1)

您已将代码置于错误的事件中。

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

答案 1 :(得分:0)

  1. 您应该为控件提供有意义的名称,以便更容易确定来自哪里。

  2. 每当填充ComboBox时,您应该先清除它。

  3. 最重要的是,您正在检查ComboBox上的SelectedIndexChanged而不是ListBox。如果你把它移动会发生什么?

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

答案 2 :(得分: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)
    {

    }

答案 3 :(得分:0)

好吧,您应该在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
 }