0

I am having issues trying to put values into another combobox after the user selects a value in the first combobox

private void ClassComboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ClassComboBox.Items.ToString().Equals("Warrior"))
        {
            RoleComboBox.Items.Add("Tank");
            RoleComboBox.Items.Add("DPS");

        }


    }

This is what I have so far since once the player choose their warrior class their roles is only allowed to be one of the following values. Problem is when I run this there are no values in my RoleComboBox. If anyone can help that would be great. Thanks

  • As a suggestion, I would look into making a class and setting the DataSource of the comboBox instead. It'll make restricting the contents of the box easier as you scale the program. Try taking a look at this: https://stackoverflow.com/questions/2417960/populating-a-combobox-using-c-sharp – Jacob JA Shanks Feb 12 '19 at 19:41
  • Does your code above actually get run? Do the values get added to `RoleComboBox.Items`, as seen in the debugger? – mmathis Feb 12 '19 at 20:02
  • @mmathis the condition was the issue here as you cannot convert an ObjectCollection to a String in this way, I'm not 100% sure why this didn't error out but the condition was never going to be true. – Jacob JA Shanks Feb 12 '19 at 20:25
  • @JacobJAShanks Whoops, I read that code wrong. Of course it doesn't error out, though, since it's comparing a string to a string. ComboBox.Items.ToString() doesn't return anything sensible (maybe just the full type), but it's a string nonetheless. – mmathis Feb 12 '19 at 20:29

1 Answers1

2
private void ClassComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ClassComboBox.SelectedItem.ToString() == "Warrior")
    {
        RoleComboBox.Items.Add("Tank");
        RoleComboBox.Items.Add("DPS");

    }


}

This might work? I'm on my cellphone so I cannot test in Visual Studio

Jacob JA Shanks
  • 370
  • 1
  • 13
Math
  • 19
  • 4
  • 2
    I would clear the list before you add the items in case this gets called a second time so you wont have duplicates. RoleComboBox.Items.Clear(); – Kevbo Feb 12 '19 at 19:59
  • @TrevorJohnson would you mind marking this as the answer if it helped you? :) – Jacob JA Shanks Feb 12 '19 at 20:22