Jump to content

How to fix code to read input from textbox in VB.NET?

- - - - -

  • Please log in to reply
7 replies to this topic

#1
tom982

tom982

    Newbie

  • Members
  • Pip
  • 3 posts
Hello all,

I am trying to learn VB.NET and one of the tests I have been set is to write an application that will read the output of a textbox, check what it is, if it is between 10 and 20 then say thanks, if not then say wrong, then clear the textbox. Sounds simple, I know, but I can't for the life of me work out what I've done wrong:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim number As Integer

        TextBox1.Text = number

        If number > 10 And number < 20 Then

            MsgBox("Thanks!")

        Else

            MsgBox("Wrong")

        End If

        Textbox1.Clear()

    End Sub

End Class


Whenever I use that, it outputs Wrong every time :( Help please!

Thanks in advance,

Tom

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Try 'MsgBox (number)' or whatever the syntax is for displaying what 'number' currently is, right before the 'if' statement.

#3
Machida

Machida

    Newbie

  • Members
  • Pip
  • 4 posts
Thats because the number variable is 0.

Do this:



  Try

    Dim number As Integer = TextBox1.Text 

  Catch

     MsgBox("Thats not a number")

  End Try


        If number > 10 And number < 20 Then

            MsgBox("Thanks!")

        Else

            MsgBox("Wrong")

        End If

        Textbox1.Clear()


Good luck :D

#4
tom982

tom982

    Newbie

  • Members
  • Pip
  • 3 posts

RhetoricalRuvim said:

Try 'MsgBox (number)' or whatever the syntax is for displaying what 'number' currently is, right before the 'if' statement.

Excuse my incompetence here, but what do you mean?

Machida said:

Thats because the number variable is 0.

Do this:



  Try

    Dim number As Integer = TextBox1.Text 

  Catch

     MsgBox("Thats not a number")

  End Try


        If number > 10 And number < 20 Then

            MsgBox("Thanks!")

        Else

            MsgBox("Wrong")

        End If

        Textbox1.Clear()


Good luck :D

That didn't work :( It outputs "Wrong" every time.

Any other ideas?

Thanks again guys

#5
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Hello,

The issue that stands out is your assignment.

        Dim number As Integer
        TextBox1.Text = number

In your code, you check the bounds of 'number', however 'number' is never assigned any value.

TextBox1.Text = number

Was this a debugging code? This should replace textbox1's text with an empty value contained in number, rather than the other way around, assigning the textbox1's text to number.

Try to correct this and see if it works.

What RR was mentioning, there are functions to display a message dialogue to show you what "number" really contains. If it always shows "wrong", then it could be your logic, or it could really be invalid input! (which is why it was mentioned that it may contain "0" instead of the real input)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#6
tom982

tom982

    Newbie

  • Members
  • Pip
  • 3 posts

Alexander said:

Hello,

The issue that stands out is your assignment.

        Dim number As Integer

        TextBox1.Text = number

In your code, you check the bounds of 'number', however 'number' is never assigned any value.

TextBox1.Text = number

Was this a debugging code? This should replace textbox1's text with an empty value contained in number, rather than the other way around, assigning the textbox1's text to number.

Try to correct this and see if it works.

What RR was mentioning, there are functions to display a message dialogue to show you what "number" really contains. If it always shows "wrong", then it could be your logic, or it could really be invalid input! (which is why it was mentioned that it may contain "0" instead of the real input)

Thank you so much for your help! It worked a treat :D

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim number As Integer

        number = TextBox1.Text

        If number > 20 Or number < 10 Then

            MsgBox("Wrong!")

        Else

            MsgBox("Thanks!")

        End If

        TextBox1.Clear()

    End Sub

End Class

No it wasn't a debugging code, it was just a mistake. Silly me! Aah okay, I get it now, thank you for explaining that for me.

Thanks again everyone who helped me out! I really appreciate it - rep all round :D

Tom

#7
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
I am very glad you have got it working, I think not a single soul will disagree if I say "programming is learned through mistakes"
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#8
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
mistakes -> experience

more experience = easier to find and correct mistakes


That's sort of what I would say.

Oh, and also, usually the best person to fix mistakes is the person who originally made the mistakes.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users