4

Simple problem: I am checking to see if a combobox has had an item selected with string.IsNullOrEmpty(). Problem is, even if is is selected, the error message appears. What am I doing wrong?

Here is my code:

private void button1Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(comboBox1.SelectedText))//here should skip to else - but doesn't
    {
        MessageBox.Show("You must select a conversion type", "Error");
    }
    else
    {
        if (comboBox1.SelectedText == "Currency")
        {
            double input = Convert.ToDouble(textBox1.Text);
            if (!string.IsNullOrEmpty(comboBox2.SelectedText))
            {
                string type = comboBox2.SelectedText;
                double result = convertCurrency(type, input);
                if (result != -1)
                {
                    label1.Text = Convert.ToString(result);
                }
             }
             else
             {
                 MessageBox.Show("You must select a conversion type", "Error");
             }
         }
         else
         {
             MessageBox.Show("curency");
         }
     } 
}

Note: This is my second ever C# program - so please don't yell at me if I'm being stupid.

F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
imulsion
  • 8,820
  • 20
  • 54
  • 84

6 Answers6

9

Generally a few observations/suggestions.

First you're using string values and are basing logic on these values, you might want to look into using an Enum and binding all it's values to the combo box. Then use the SelectedItem property and compare it to the Enum.

When nothing is selected the SelectedItem will return NULL, another option is using SelectedIndex which will return -1 when no item has been selected.

So with the SelectedIndex it would become something like;

if (comboBox1.SelectedIndex == -1)//Nothing selected
{
    MessageBox.Show("You must select a conversion type", "Error");
}
else
{
    //Do Magic   
}

Generally using string comparisons should only be done when anything "strong" like an int comparison or even better an enum comparison is not possible. (Maybe it's just me though but strings change to often and are just scary for this sort of stuff.)

For the enum suggestion possibly look at one of these links;

Binding an enum to a WinForms combo box, and then setting it

Load values of enum type into a combobox

Is it possible to load items from an Enum to a ComboBox in .NET 3.5?

Binding a ComboBox to an Enumeration

I'm not sure which .NET version and things you're using as binding is alot easier in WPF then in the old windows form (in my opinion).

Community
  • 1
  • 1
F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
1
private void button2_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex==-1)
    {
        MessageBox.Show("You must select a conversion type", "Error");
    }
    else
    {
        MessageBox.Show("Selected");
    }

}
F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
Vijay Hulmani
  • 969
  • 8
  • 17
1

from the MSDN doc, this answers your question exactly

You can use the SelectedText property to retrieve or change the currently selected text in a ComboBox control. However, you should be aware that the selection can change automatically because of user interaction. For example, if you retrieve the SelectedText value in a button Click event handler, the value will be an empty string. This is because the selection is automatically cleared when the input focus moves from the combo box to the button.

F.B. ten Kate
  • 2,032
  • 2
  • 21
  • 31
Joe Vi
  • 473
  • 4
  • 10
1

Another solution: You can check whether the combobox has had an item selected by checking a condition with an Index which is out of range (which item is impossible to have), then, if it is not having a possible value, you can set it manually.

if (comboBox1.SelectedIndex == -1) //index out of range
    {
        //show the error message 
        comboBox1.SelectedIndex = 1; //set the first value to the combo box
    }
    else
    {
        //continue   
    }
MrMalith
  • 1,352
  • 14
  • 22
0

Poor naming on microsoft's part. You should use comboBox.Text to get what you are looking for.

comboBox.SelectedIndex the index of the selected value

comboBox.SelectedItem if you using a DataSource, this is the item seleted in the datasource

comboBox.SelectedValuethe ValueMember of the Datasource or the currently selected value if you added your own Items

comboBox.Text The text that you see, even if it is not in the list

.SelectedText, refers to and text selected (highlighted) within the .Text

0
private void Resetbtn_Click(object sender, EventArgs e)
{    
    comboBox1.Items.Clear();
    //Student is a combobox elements.Add again.
    comboBox1.Items.Add("Student");  
    comboBox1.Items.Add("Staff");

}
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63