Reputation: 107
I am attempting to create an order form and as such am using combo boxes in order to let the user choose what item is going to be ordered. As such when the user selects the item that is going to be ordered, the second combo box should change to the sizes that the specific item can be ordered in. I have filled the second combo box with the sizes for all the items but I am unsure as to how to limit the sizes per the item that is selected. I have tried using if statements to addRange to the second combo box however this just duplicates the items at the end of the combo box. any help that can be given on this would be greatly appreciated. Thanks
private void itemBox_SelectedIndexChanged(object sender, EventArgs e)
{
switch (((ComboBox)sender).SelectedItem as string)
{
case "Name in a Frame":
sizeBox.SelectedIndex = 0;
break;
case "Scrabble Frame":
sizeBox.SelectedIndex = 1;
break;
case "Plaque":
sizeBox.SelectedIndex = 2;
break;
case "Hearts":
sizeBox.SelectedIndex = 3;
break;
case "Now and Forever Print":
sizeBox.SelectedIndex = 4;
break;
case "Pat cushion":
sizeBox.SelectedIndex = 5;
break;
case "Emilia cushion":
sizeBox.SelectedIndex = 6;
break;
}
}
private void sizeBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (sizeBox.SelectedIndex == 0)
{
this.sizeBox.Items.AddRange(new object[]{
"7x5",
"10x8",
"A4",
"Mug"
});
}
}
Upvotes: 2
Views: 2008
Reputation: 70718
You could just populate the sizeBox collection directly from the itemBox selected change event handler and remove sizeBox_SelectedIndexChanged
altogether.
However, to achieve what, you need to clear the items in the sizeBox once the item has been selected. You can achieve this via:
sizeBox.Items.Clear();
You can then add the items once the sizeBox selected index has changed. I would simply use:
sizeBox.Items.Add("New Size");
For good practice I would remove the use of magic strings, maybe put them in a Products helper class that returns the appropriate string.
Upvotes: 3