简体   繁体   English

从列表框 c# 中选择参数时组合框不显示项目

[英]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.我希望每次我从 ListBox 中选择某些内容时,我的 ComboBox 都显示一组参数,但它没有在 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这是输出

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
        }   
  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.每当填充 ComboBox 时,您应该先清除它。

  3. Most importantly, you are checking for the SelectedIndexChanged on the ComboBox instead of the ListBox.最重要的是,您正在检查 ComboBox 而不是 ListBox 上的SelectedIndexChanged 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)
{
}
 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)
    {

    }

Well, you should update ( remove old and add new) comboBox1.Items on listBox4 change:好吧,您应该更新(删除旧的并添加新的) 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();
     }  
   }

And it seems that comboBox1 is of no use, at least now :而且似乎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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM