Solved

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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