Hendra Anggrian
Hendra Anggrian

Reputation: 5848

How do I detect if no selected item on ComboBox is chosen?

In my ComboBox, the field is blank before users click it and choose any item. So without users click on the ComboBox, it remains empty. How do we check whether the ComboBox is empty or not?

This codes gives me an error because there is no item selected yet:

if( ComboBox.SelectedItem.ToString().Equals("") )
{
     //do something
}

Upvotes: 16

Views: 66567

Answers (3)

Geiziry
Geiziry

Reputation: 23

if( ComboBox.SelectedIndex == -1 )

Upvotes: -1

Jcl
Jcl

Reputation: 28272

if( ComboBox.SelectedItem == null ) {
   // do something
}

Upvotes: 45

inzenir
inzenir

Reputation: 116

ComboBox.SelectedItems.Count

this should work :P it counts selected items. if that number is 0, no items are selected.

Upvotes: 3

Related Questions