• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

compare Textbox input to Int variables and display result

I'm fairly new to VB.net. I’m trying to create a hypothetical project. please see attachments!

I have a win-form (see car2 attachment) with x3 textboxes:
Textbox1 = Car-Len
Textbox2 = Car-Height
Textbox3 = Car-Width
and lots of labels.

The idea is:
input car Length, Width, Height in to their textboxes and if (for example: London Garage has a value greater than – say Length, then Y appears in a Label next to London and same for other two boxes.  Hope that made sense.

So in a nut shell:  I put dimensions of a car into Textboxes and if compared to Garage London dimensions, then label(s) say yes or no... so, basically, does the car fit in garage - phew!!!
Below is my shocking attempt at code (WHICH DOESN'T WORK) (i suspect it's something to do with string to int conversion - I'm just guessing!
  Dim LondonH As Integer = 5 ' 8 feet
    Dim LondonW As Integer = 5 ' 9 to 10 feet wide
    Dim LondonL As Integer = 13 '18 to 20 feet long
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Label1.Text = "London"
        Label2.Text = "Paris"
        Label3.Text = "Italy"
    End Sub

    Private Sub TextBox3_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox3.TextChanged
        ' len
        If Val(TextBox1.Text) < LondonL Then
            Label7.Text = "n"
        ElseIf Val(TextBox1.Text) > LondonL Then
            Label7.Text = "y"
        End If
    End Sub

Open in new window

Many thanks,
Robert Freeman
Robert Freeman
2 Solutions
Ryan ChongCommented:
instead of Val, use:

Éric MoreauSenior .Net ConsultantCommented:
One thing I see is that this event is handling the TextBox3 TextChanged event and inside the event handler, you are using TextBox1. This does not look right to me!
Fernando SotoRetiredCommented:
Hi Robert;

Please try code like this.
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged

    '' Reset the control
    Label7.Text = ""
    '' TryParse will fill this variable with the converted value
    Dim len = -1

    '' Make sure there is something to test in the TextBox1
    If String.IsNullOrWhiteSpace(TextBox1.Text) Then Return

    '' Test the value in TextBox1 and take the correct action
    If Int32.TryParse(TextBox1.Text, len) Then
        If len <= LondonL Then
            Label7.Text = "y"
            Label7.Text = "n"
        End If
        '' Invalid text value found in the TextBox1
        MessageBox.Show("Invalid character was entered. Please correct.")
    End If

End Sub

Open in new window

David Johnson, CD, MVPOwnerCommented:
Public Class Form1
    Dim LondonH As Integer = 8
    Dim LondonW As Integer = 10
    Dim LondonL As Integer = 20
    Dim London As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        London = "N"

            If ((CInt(TextBox1.Text) < LondonH) And (CInt(TextBox2.Text) < LondonW) And (CInt(TextBox3.Text) < LondonL)) Then
                London = "Y"
            End If
            Label7.Text = London
            MessageBox.Show("Length Width Height must be Entered as Whole Numbers")
        End Try
    End Sub
End Class

Open in new window

Fernando SotoRetiredCommented:
Most complete answers.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now