Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if item is selected from a comboBox in C#

Tags:

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. :)

like image 616
Gil Peretz Avatar asked Apr 16 '11 16:04

Gil Peretz


People also ask

How do I get the ComboBox selected value?

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.

What is selected item in ComboBox?

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.

How do you check if an item already exists in a ComboBox C#?

Contains("Combo") you have to add strings to your ComboBox, not ComboBoxItems: cb. Items. Add("Combo") . The string will display just like a ComboBoxItem.


1 Answers

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; 
like image 73
Roy T. Avatar answered Sep 29 '22 16:09

Roy T.