Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

23 Experts available now in Live!

Get 1:1 Help Now