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
}
int a = ComboBox. SelectedIndex. CompareTo(-1); if (a == 0) { MessageBox. Show("Please select something."); } else { // do something if combo box selection is done.! }
You can use "ComboBoxItem. PreviewMouseDown" event.
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(comboBox1.
if( ComboBox.SelectedItem == null ) {
// do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With