C# 列表框1\u选择的索引已更改

C# 列表框1\u选择的索引已更改,c#,listbox,selecteditem,C#,Listbox,Selecteditem,我想做的是,当在listBox1中选择一个项目时,listBox2将填充可供选择的选项。例如,如果在listBox1中选择一件白色衬衫,则listBox2将填充可供选择的设计。我已经看了100遍了,从我所读到的来看,它应该是有效的,但它根本不起作用。唯一有效的方法是用衬衫颜色填充listBox1。任何帮助都将非常感激 using System; using System.Windows.Forms; namespace EmmasEmbroidery { public partial class

我想做的是,当在listBox1中选择一个项目时,listBox2将填充可供选择的选项。例如,如果在listBox1中选择一件白色衬衫,则listBox2将填充可供选择的设计。我已经看了100遍了,从我所读到的来看,它应该是有效的,但它根本不起作用。唯一有效的方法是用衬衫颜色填充listBox1。任何帮助都将非常感激

using System;
using System.Windows.Forms;
namespace EmmasEmbroidery
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        listBox1.Items.Add("White");
        listBox1.Items.Add("Black");
        listBox1.Items.Add("Red");
        listBox1.Items.Add("Blue");
    }


    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox1.Enabled = true;
        listBox2.Items.Clear();

        if (listBox1.SelectedItem.Equals("White"))
        {
            listBox2.Items.Add("Peacock");
            listBox2.Items.Add("Palm Tree");
            listBox2.Items.Add("Rose");
        }
        else if (listBox1.SelectedItem.Equals("Black"))
        {
            listBox2.Items.Add("Race Car");
            listBox2.Items.Add("Star");
            listBox2.Items.Add("Moon");
        }
        else if (listBox1.SelectedItem.Equals("Red"))
        {
            listBox2.Items.Add("Palm Tree");
            listBox2.Items.Add("Moon");
        }
        else if (listBox1.SelectedItem.Equals("Blue"))
        {
            listBox2.Items.Add("eacock");
            listBox2.Items.Add("Moon");
        }

        label3.Text = "You have selected a " + listBox1.SelectedItem.ToString() + " shift";
    }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        listBox2.Enabled = false;
        label3.Text = "You have selected a " + listBox1.SelectedItem.ToString() + " shift with a " + listBox2.SelectedItem.ToString() + " design.";
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        listBox1.Enabled = true;
        listBox2.Enabled = true;

        listBox1.Items.Clear();
        listBox2.Items.Clear();
        label3.Text = "";

        listBox1.Items.Add("White");
        listBox1.Items.Add("Black");
        listBox1.Items.Add("Red");
        listBox1.Items.Add("Blue");
    }
}
}

根据MSDN的使用

// Allow the ListBox to repaint and display the new items.
   listBox2.EndUpdate();

给定的代码工作得非常好 据我所知,问题出在事件中,您一定是复制了代码,忘记了将事件添加到列表框中。您甚至可以通过属性将事件添加到列表框中