Validating if a combobox it's SelectedText property is empty always fails

imulsion

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

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't save selected Combobox Item because SelectedText is empty String. Why?

From Dev

Sequalize validating email, fails with Empty String

From Dev

Validating a ListBox's contents are not empty

From Dev

Validating a ListBox's contents are not empty

From Dev

HtmlAgilityPack - Attributes property always empty

From Dev

if(!buydat.empty()) validation fails (always true)

From Dev

ApplicationContext property always return an Empty dictionary

From Dev

.Text property of TextBox with CalendarExtender is always empty

From Dev

Scipy's griddata method always fails

From Dev

Validating TinyMCE for empty inputs

From Dev

Validating empty $_FILES

From Dev

Validating TinyMCE for empty inputs

From Dev

Why user's activities is always empty?

From Dev

chrome extension: Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

From Dev

chrome extension: Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

From Dev

Validating children of a collection property

From Dev

MeasureOverride not always called on children's property changes

From Dev

Validating fails because of account permissions

From Dev

EXTJS 6 : Set ComboBox's value when its store is empty

From Dev

How to splice an empty property of an object that's into an array?

From Dev

How to dynamically set jquery multiselect option's selectedText in javascript ..?

From Dev

How to dynamically set jquery multiselect option's selectedText in javascript ..?

From Dev

empty() check not validating after POST

From Dev

Jquery validating not empty and is URL in forms

From Dev

SelectedIndex of a ComboBox is always -1

From Dev

breeze: validating navigation property on change

From Dev

Validating Attached Property in Property Change handler

From Dev

Delphi's TPerlRegEx.EscapeRegExChars() always return an empty string?

From Dev

Always displaying the first row of a DataGrid, even if it's empty?

Related Related

  1. 1

    Can't save selected Combobox Item because SelectedText is empty String. Why?

  2. 2

    Sequalize validating email, fails with Empty String

  3. 3

    Validating a ListBox's contents are not empty

  4. 4

    Validating a ListBox's contents are not empty

  5. 5

    HtmlAgilityPack - Attributes property always empty

  6. 6

    if(!buydat.empty()) validation fails (always true)

  7. 7

    ApplicationContext property always return an Empty dictionary

  8. 8

    .Text property of TextBox with CalendarExtender is always empty

  9. 9

    Scipy's griddata method always fails

  10. 10

    Validating TinyMCE for empty inputs

  11. 11

    Validating empty $_FILES

  12. 12

    Validating TinyMCE for empty inputs

  13. 13

    Why user's activities is always empty?

  14. 14

    chrome extension: Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

  15. 15

    chrome extension: Stack trace: TypeError: Cannot read property 'selectedtext' of undefined

  16. 16

    Validating children of a collection property

  17. 17

    MeasureOverride not always called on children's property changes

  18. 18

    Validating fails because of account permissions

  19. 19

    EXTJS 6 : Set ComboBox's value when its store is empty

  20. 20

    How to splice an empty property of an object that's into an array?

  21. 21

    How to dynamically set jquery multiselect option's selectedText in javascript ..?

  22. 22

    How to dynamically set jquery multiselect option's selectedText in javascript ..?

  23. 23

    empty() check not validating after POST

  24. 24

    Jquery validating not empty and is URL in forms

  25. 25

    SelectedIndex of a ComboBox is always -1

  26. 26

    breeze: validating navigation property on change

  27. 27

    Validating Attached Property in Property Change handler

  28. 28

    Delphi's TPerlRegEx.EscapeRegExChars() always return an empty string?

  29. 29

    Always displaying the first row of a DataGrid, even if it's empty?

HotTag

Archive