I'm pretty new here.
I have a form, and want to check if the user filled it in correctly. In the form there's a combo box; how can I build the "if" statement for checking whether the user picked an item from it ?
P.S. Sorry for my bad English, it's not my mother tongue. :)
The two primary methods to display and get the selected value of a ComboBox are using Combobox. SelectedItem and ComboBox. GetItemText properties in C#. A selected item's value can be retrieved using the SelectedValue property.
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index.
Contains("Combo") you have to add strings to your ComboBox, not ComboBoxItems: cb. Items. Add("Combo") . The string will display just like a ComboBoxItem.
Use:
if(comboBox.SelectedIndex > -1) //somthing was selected
To get the selected item you do:
Item m = comboBox.Items[comboBox.SelectedIndex];
As Matthew correctly states, to get the selected item you could also do
Item m = comboBox.SelectedItem;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With