Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if combobox value is empty

I have created a ComboBox with three values. I wanted that a message box opens when no item is selected so I tried this:

if (comboBox1.SelectedItem == null)
{
    MessageBox.Show("Please select a value");
    return;
}

That works fine but only if I click into the field in the combobox. When I dont touch it, the program will start without message box. Whats wrong?

like image 588
uzi42tmp Avatar asked Nov 06 '14 07:11

uzi42tmp


People also ask

How do I know if my ComboBox is empty?

Use string. IsNullOrEmpty(comboBox1. Text) to handle all input/selection cases.

How check ComboBox is empty or not in C#?

text = =null and comboBox. SelectedItem = = null doest work properly. If some one can tell me,its apreciated.

How to check if ComboBox is empty or not in Powerapps?

If you want to determine there is nothing selected and nothing searched within Combo Box, you should use the And operator to combine the conditions that check Combo Box selected and Combo Box search text.

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.

How to check if a combobox value is null or empty?

IsNull (ComboBox1) and IsNull (ComboBox1).Value will both never be true. Null is a value returned from a database if a field contains no value. You have to check if the value of the ComboBox is empty. An empty string in VBA is a string with the length 0, so you have to use on of those: If Me.ComboBox1 = "" then ...

How to select items from a list in combobox?

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. Conditionally testing SelectedItem or SelectedIndex will not handle the case of the user entering a new value from another input device like a keyboard. Use string. IsNullOrEmpty.

Does while filter work with combobox1 selected?

While filters do work using Combobox1.Selected.Value, IsBlank or IsEmpty show false, even if empty. I need it to work because I want to display a banner that says "filters are active" when at least one filter is triggered.

What is combobox in Salesforce?

A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. Conditionally testing SelectedItem or SelectedIndex will not handle the case of the user entering a new value from another input device like a keyboard.


2 Answers

if (string.IsNullOrEmpty(comboBox1.Text)) or if (comboBox1.SelectedIndex == -1)

like image 82
free4ride Avatar answered Sep 19 '22 16:09

free4ride


Use

if (comboBox1.SelectedIndex == -1)
{
   MessageBox.Show("Please select a value");
   return;           
}

Note: SelectedIndex will be set to -1 when SelectedValue is blank ONLY when FormattingEnabled is true. See here.

like image 27
Octane Avatar answered Sep 18 '22 16:09

Octane