1

I would like to do a comparison of :
ComboBox.SelectedItem which is of the object type to the string.Empty in combination with "||"

If I run with above command I get an error:

"Operator '||' cannot be applied to operands of type 'object' and 'bool' "

Code

if (comboBox2.SelectedItem || comboBox1.SelectedItem == string.Empty )
Parnal p
  • 40
  • 6

3 Answers3

3

Try this:

if ((comboBox1.SelectedItem?.Equals(string.Empty) ?? false)
    || (comboBox2.SelectedItem?.Equals(string.Empty) ?? false))
Antoine V
  • 6,998
  • 2
  • 11
  • 34
0

Try this

if((comboBox1.SelectedItem==null || comboBox1.SelectedItem == string.Empty) || (comboBox2.SelectedItem == null || comboBox2.SelectedItem == string.Empty))
{
     MessageBox.Show("Select Item!");
}
Amy
  • 35
  • 12
0

You can't compare bool and object (selected item of a combo box) based on @ThierryV answer you can define an function like this for check your condition :

private bool CheckEmptyComboBox(ComboBox comboBox)
{
    return (comboBox.SelectedItem==string.Empty ?? false)
}

and then should use this function in your if statement condition:

if( CheckEmptyComboBox(comboBox1) || CheckEmptyComboBox(comboBox2) || CheckEmptyComboBox(comboBox3) ...)

also you can use a foreach statement to find all Combo boxes in a panel or group box

Hamed Naeemaei
  • 8,052
  • 3
  • 37
  • 46