9

I have a combo box in which I have to display the dates from a database. The user has to select a date from the combo box to proceed further, but I don't know how to make the user aware of selecting the item from the combo box first in order to proceed further.

What process should be followed so that a user can get a message if he has not selected the date from the combo?

meJustAndrew
  • 6,011
  • 8
  • 50
  • 76
zoya
  • 473
  • 4
  • 11
  • 21

9 Answers9

14
if (string.IsNullOrEmpty(ComboBox.SelectedText)) 
{
 MessageBox.Show("Select a date");
}
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
  • 2
    I know this post is old, but as I stumbled this, some might stumble as well. In my case the 'ComboBox.SelectedText' didn't work, I had to use 'ComboBox.Text'. I only checked that there is one other answer with my approach, but appart from my UP to that answer, here might catch the eye more easily. – StinkyCat Mar 04 '13 at 15:45
  • 1
    @StinkyCat is right... I kept getting a null value when using 'SelectedText' or 'SelectedValue' but simply using 'Text' works. – KingOfAllTrades Aug 26 '15 at 16:47
  • its easier to use Combobox.SelectedItem.ToString so that you have an idea of what was selected or if nothing was selected at all. but thanks for the answer this helped. – JT4U Aug 12 '16 at 15:28
4

Here is the perfect coding which checks whether the Combo Box Item is Selected or not:

if (string.IsNullOrEmpty(comboBox1.Text))
{
    MessageBox.Show("No Item is Selected"); 
}
else
{
    MessageBox.Show("Item Selected is:" + comboBox1.Text);
}
shanethehat
  • 15,460
  • 11
  • 57
  • 87
Gokul
  • 137
  • 1
  • 3
3

You can use this:

if (Convert.ToInt32(comboBox1.SelectedIndex) != -1)
{
    // checked
}
else
{
    // unckecked
}
eduardomozart
  • 1,444
  • 15
  • 14
2

You'll want to use DropDownStyle = DropDownList so you can easily make sure that the user picked an entry from the list and can't type random text in the box. Add an empty item to Items before you populate it (or "Please select"). Now, the default is automatically empty and the test is simple: just check that SelectedIndex > 0.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

check the text property like this

if (combobox.text != String.Empty)
{
//continue
}
else
{
// error message
}
Wael Dalloul
  • 22,172
  • 11
  • 48
  • 57
1
if (cboDate.SelectedValue!=null)
{
      //there is a selected value in the combobox
}
else
{
     //no selected value
}
NET Experts
  • 1,525
  • 17
  • 35
1
if(combobox.Selectedindex==-1)
{
MessageBox.Show("Please Select an item");
}

else
{
MessageBox.Show("An Item was selected");
}
Adumuah Dowuona
  • 121
  • 1
  • 11
0

Pl. note ComboBox.Text only checks for the Text that is at the editable region of the ComboBox, so that's not supposed to be used when you want to check if there's some selection from within the ComboBox.

This will work always.

        int a = ComboBox.SelectedIndex.CompareTo(-1);

        if (a == 0)
        {
            MessageBox.Show("Please select something.");
        }
        else
        {
            // do something if combo box selection is done.!
        }
jaymeht
  • 678
  • 1
  • 8
  • 14
0

You can use SelectedIndex or SelectedItem properties of the ComboBox.

Andrew Bezzub
  • 15,744
  • 7
  • 51
  • 73