-1

what I am trying to do, when an item is selected in listBox1, then listBox2 will populate with options to select from. For example if you pick a white shirt in listBox1 then listBox2 will populate with designs to choose from. I have gone over this a 100 times and from what I have read it should work but it is not working at all. The only thing that works is listBox1 populates with shirt colors. Any help would be greatly appriciated.

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");
    }
}

}

LRoyster
  • 43
  • 4
  • 9

2 Answers2

1

The given code is working perfectly fine enter image description here as far as i know the problem is in the event, you must have copied the code and forgot to add event to the list box.You can add even to listbox by properties

enter image description here

Usman
  • 4,615
  • 2
  • 17
  • 33
0

Acording MSDN use

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

https://msdn.microsoft.com/pt-br/library/system.windows.forms.listbox.items(v=vs.90).aspx

fdfey
  • 587
  • 2
  • 11