-1

I see lots of posts on this but not using ints I have the line converting the int declaring the grade as the value which is chosen in the combo_grade but my if statment isnt working, im trying to have combo_sub show the message if the combo_grade is equal to 9.

private void combograde_SelectedIndexChanged(object sender, EventArgs e)
{
    if (combograde.SelectedValue == "9")
    {
        combosub.Items.Add("If this shows it works");
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Marko
  • 1
  • 1
  • Why do you declare integer "grade" but you're not using it ? – Florian Feb 12 '18 at 09:25
  • int grade is supposed to be set as the number which the user selects from combo_grade – Marko Feb 12 '18 at 09:26
  • 1
    You have a value which is 9 in your combobox grade, right ? I see your method's name is "comboBox3_SelectedIndexChanged". You should clarify, do you have a combobox named "comboBox3"? – Florian Feb 12 '18 at 09:28
  • i have changed combo box 3 to combo_sub in properties, and yes i have 9 set in combo_grade – Marko Feb 12 '18 at 09:30
  • then you have the wrong `SelectedIndexChanged` event. if you select in `combo_grade` then you need to register the `SelectedIndexChanged` to `combo_grade` and put your code in there – Mong Zhu Feb 12 '18 at 09:32
  • Then your problem is here ! :) You need to handle the event "SelectedIndexChanged" on your control "combo_grade". So, create a method : "private void combo_grade_SelectedIndexChanged(object sender, EventArgs e). Make sure that you have a reference (VisualStudio should add the listener automatically in your designer class). – Florian Feb 12 '18 at 09:33
  • + you should respect PascalCase convention for naming, that means do not use underscore on controls, instead of combo_grade, prefer comboBoxGrade (but this is not the point here. Let us know if you have some troubles to make the code works!) – Florian Feb 12 '18 at 09:36
  • now this is my code however still not having the text show in combosub when combograde = 9 // I have updated my code – Marko Feb 12 '18 at 09:45
  • please look for all references of `combograde_SelectedIndexChanged` and post the line (or screenshot) of the registration of this event: `this.combograde.SelectedIndexChanged += new System.EventHandler(combograde_SelectedIndexChanged...` – Mong Zhu Feb 12 '18 at 10:00
  • This look better ! MongZhu and I are suspecting that your method is not fireing. To fire your method `combograde_SelectedIndexChanged`, you need to add on your controller, an event handler. VisualStudio is able to do it automatically in the *ClassName*.Designer.cs, but sometimes it is not auto-generated and you need to do it yourself. – Florian Feb 12 '18 at 10:05
  • I have found a nice article that might help you understand what are events, handlers, etc. https://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp – Florian Feb 12 '18 at 10:08

1 Answers1

2

tl;dr use combograde.SelectedItem instead of combograde.SelectedValue

I did a little digging around in VS and on MDSN. From the looks of it SelectedValue is only useful when using data binding. It displays the value of the field denoted by ValueMember which selects the value to be displayed by complex data.

MDSN seems to suggest that when ValueMember is null or nothing SelectedValue will be to the same as SelectedItem but that isn't what I observed.

**Previous Answer**

If combo_sub is to have the message shouldn't

combo_grade.Items.Add("If this shows it works");

be

combo_sub.Items.Add("If this shows it works");
Michael
  • 810
  • 6
  • 18
  • 2
    This should be a comment, but I don't have enough rep to comment yet... – Michael Feb 12 '18 at 09:21
  • Ah blind mistake from me, even though I fixed the error the combox is still not showing the set options for the combo_grade in the combo_sub – Marko Feb 12 '18 at 09:24
  • @Marko then it seems that the `SelectedIndexChanged` event is registered to `comboBox3`, but it should be registered to `combo_grade` – Mong Zhu Feb 12 '18 at 09:31