Solved

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

Posted on 2014-03-05
4
404 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
  • 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 85

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now