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

59,868

Solution 1

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

Solution 2

ComboBox.SelectedItems.Count

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

Share:
59,868
Hendra Anggrian
Author by

Hendra Anggrian

Open source enthusiast in awe of Kotlin technologies. Spent most times following new stuff and best practices in GitHub.

Updated on February 15, 2020

Comments

  • Hendra Anggrian
    Hendra Anggrian over 4 years

    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
    }
    
  • Jcl
    Jcl over 11 years
    That's not guaranteed to work when SelectionMode is Single. It'll probably work, but documentation doesn't state that. From MSDN: When SelectionMode is Multiple or Extended, use the SelectedItems property to get the items that are selected. When SelectionMode is Single, use the Selector.SelectedItem property to get the item that's selected.