Solved

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

Posted on 2014-03-05
4
406 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 47
Get the selected ValueMember of Combobox 5 31
.NET 2008 VB and C# 6 28
Sending receiving text messages in vb.net 15 38
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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