2

This is how I check if a user input a empty in comboBox

if (string.IsNullOrEmpty(comboBox.Text))
{
MessageBox.Show("No Item is Selected"); 
}

How to check if user input are in the comboBox items? For example the comboBox items are a,b,c. When the user input "d" in the comboBox then he leaves, a messageBox must show.

Square Ponge
  • 702
  • 2
  • 10
  • 26

3 Answers3

4

You can try putting something like this in your ComboBox's Leave EventHandler as George stated, checking if the item is contained in the ComboBox's Item Collection.

private void comboBox1_Leave(object sender, EventArgs e)
{
    ComboBox cb = (ComboBox)sender;
    if (! cb.Items.Contains(cb.Text))
    {
        MessageBox.Show("No Item is Selected");
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
2

Try this:

int resultIndex = -1;
resultIndex = comboBox.FindExactString("d");

if(resultIndex == -1)
{
    MessageBox.Show("No Item is Selected");
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
1

In this case, the answer from @Mark Hall is correct. But if you want to restrict the user to not use a item which isn't in the item collection from the combo box, I suggest you to turn the the property of DropDownStyle to DropDownList.

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
Sornii
  • 421
  • 3
  • 11