繁体   English   中英

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

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

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

这是输出

您将代码放在错误的事件中。

        // 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. 您应该为控件指定有意义的名称,以便更容易确定来自何处的内容。

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

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

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

    }

好吧,您应该更新(删除旧的并添加新的) 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
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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