Solved

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

Posted on 2014-03-05
4
410 Views
Last Modified: 2014-03-05
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

0
Comment
Question by:printmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Assisted Solution

by:5teveo
5teveo earned 250 total points
ID: 39907380
try

each field must be tested for integer (numeric)

If not isnumeric(txtQty4.text) then
    txtQty4.text = 0
end if

If not isnumeric(txtQty2.text) then
    txtQty2.text = 0
end if

If cint(txtQty4.Text) <= cint(txtQty2.Text)  then
.
.
.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 39907547
Here is a robust example using Integer.TryParse():
        Dim Qty2 As Integer
        Dim Qty4 As Integer
        If Integer.TryParse(txtQty2.Text, Qty2) Then
            If Integer.TryParse(txtQty4.Text, Qty4) Then
                If Qty4 <= Qty2 Then
                    ' ... clear out the fields ...
                Else
                    ' ... do something with "Qty4" and "Qty2"? ...
                End If
            Else
                MessageBox.Show("Invalid Quantity4")
            End If
        Else
            MessageBox.Show("Invalid Quantity2")
        End If

Open in new window

0
 
LVL 40
ID: 39907747
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.
0
 
LVL 8

Expert Comment

by:5teveo
ID: 39907936
Thanks for points!

Happy coding!
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question