Link to home
Start Free TrialLog in
Avatar of printmedia
printmedia

asked on

Textbox content not evaluated as number but as string in vb.net windows form

Hi all.

I have a windows form that has various textboxes, two of these textboxes are for entering quantities. Below is a portion of the script that does a comparison between both textboxes, what I'm realizing is that it is not comparing them as numbers but as string. When txtQty4.Text is 999 and txtQty2 is 1000 the comparison fails and it shouldn't.

How can I get it to treat them as numbers? Thank you in advance.

ElseIf txtQty4.Text <= txtQty2.Text Then

            txtQty3.Text = Nothing
            txtQty4.Text = Nothing
            txtQty5.Text = Nothing
            txtQty6.Text = Nothing
            txtQty7.Text = Nothing
            txtQty8.Text = Nothing
            txtQty9.Text = Nothing
            txtQty10.Text = Nothing

            txtPrice2.Text = Nothing
            txtPrice3.Text = Nothing
            txtPrice4.Text = Nothing
            txtPrice5.Text = Nothing

            txtQty4.Enabled = False
            txtQty6.Enabled = False
            txtQty8.Enabled = False
            txtQty10.Enabled = False

            txtPrice2.Enabled = False
            txtPrice3.Enabled = False
            txtPrice4.Enabled = False
            txtPrice5.Enabled = False

Open in new window

SOLUTION
Avatar of 5teveo
5teveo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
TryParse is a better choice than IsNumeric, because you associate with a specific type of numeric value, such as Integer in Mike example, while the IsNumeric in the first example will return anything that is a number, which might not be interesting for a quantity, because it will also accept decimals.

However, be aware that Integer is not the only type of data that has a TryParse. All the base types (Integer, Double, Date) have a TryParse, that make the distinction between what the different types accept.

For instance, if your quantity must never be negative and is never higher than 255, Byte.TryParse might be more interesting because it will not accept negative values, while Integer.TryParse will.
Thanks for points!

Happy coding!