[RESOLVED] [2005] String question..
Hello,
I'm making a program in which I want only integers to be entered into the textbox. I tried making a messagebox appear if the text entered into the textbox is not an integer, but I can't seem to get it right. How do you make an alert telling the user to enter an integer if they enter a string, or decimal, etc., etc. into the textbox?
Thanks!
Regards,
Silencer
Re: [2005] String question..
I also want to make an error messagebox show up when there is no text in textbox1, and not get a non-responsive program. Here is my code:
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim agerange As Integer
agerange = TextBox1.Text
Select Case agerange
Case 1 To 3
MessageBox.Show("You are a Toddler", "You are a Toddler", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 4 To 10
MessageBox.Show("You are a Youngster", "You are a Youngster", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 11 To 12
MessageBox.Show("You are a Pre-teen", "You are a Pre-teen", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 13 To 19
MessageBox.Show("You are a Teenager", "You are a Teenager", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 20 To 29
MessageBox.Show("You are in your twenties", "You are in your twenties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 30 To 39
MessageBox.Show("You are in your thirties", "You are in your thirties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 40 To 49
MessageBox.Show("You are in your forties", "You are in your forties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 50 To 59
MessageBox.Show("You are in your fifties", "You are in your fifties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 60 To 69
MessageBox.Show("You are in your sixties", "You are in your sixties", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
Me.Refresh()
TextBox1.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
Me.Button2.Text = "&How Old Are You?"
Me.Button1.Text = "&Exit"
End Sub
End Class
Re: [2005] String question..
There are numerous threads dedicated to preventing non-numeric input in a TextBox. Of course, the most logical way to get numerical input from the user is with a NumericUpDown control. If you are going to use a TextBox and you want to reject invalid input as it's typed then I suggest you consult those existing threads. If you prefer to validate after the fact then the Integer.TryParse method is for you.
Re: [2005] String question..
Thanks, how would I put the Integer.tryparse into my code above, and also make an error messagebox show up when there is no text in textbox1, and not get a non-responsive program.
Re: [2005] String question..
What does the Integer.TryParse method do? Have you checked? If not then I suggest that you do because you can probably work it out for yourself. That would be preferable, would it not? My philosophy is "try first, then ask if you fail". I tend to offer guidance rather than outright solutions in many cases in the hope that others will do the same.
Re: [2005] String question..
It converts the string representation of a number in a specified style and culture-specific format to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Dim agerange As Integer
Dim s As String
Dim style As Globalization.NumberStyles
Dim provider As IFormatProvider
Dim result As Integer
Dim returnValue As Boolean
returnValue = Integer.TryParse(s, style, provider, result)
thats how you would use it, but I don't know what to assign provider and s...I was thinking s is textbox1.text, but not sure.
Re: [2005] String question..
I tried using your code which you posted in another post, and it worked, but after I hit ok in the messagebox that said the value you have entered is not a valid number, it gave me a nonresponsive program...
vb Code:
Dim number As Double
If Double.TryParse(TextBox1.Text, number) Then
'The text is a valid number.
TextBox1.Text = number.ToString("n2") 'Format as a number with 2 decimal places.
Else
MessageBox.Show("The value entered is not a valid number.")
TextBox1.Clear()
End If
Re: [2005] String question..
So far this is my code, with your code ammended at the top, I disregarded the Integer.tryparse method, it seemed unfit for the problem. This method you had, works, but I still get an error, with the piece of text that says
"agerange = Textbox1.text"
vb Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim number As Double
If Double.TryParse(TextBox1.Text, number) Then
'The text is a valid number.
TextBox1.Text = number.ToString("n2") 'Format as a number with 2 decimal places.
Else
MessageBox.Show("The value entered is not a valid number.")
TextBox1.Clear()
End If
Dim agerange As Integer
agerange = TextBox1.Text
Select Case agerange
Case 1 To 3
MessageBox.Show("You are a Toddler", "You are a Toddler", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 4 To 10
MessageBox.Show("You are a Youngster", "You are a Youngster", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 11 To 12
MessageBox.Show("You are a Pre-teen", "You are a Pre-teen", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 13 To 19
MessageBox.Show("You are a Teenager", "You are a Teenager", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 20 To 29
MessageBox.Show("You are in your twenties", "You are in your twenties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 30 To 39
MessageBox.Show("You are in your thirties", "You are in your thirties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 40 To 49
MessageBox.Show("You are in your forties", "You are in your forties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 50 To 59
MessageBox.Show("You are in your fifties", "You are in your fifties", MessageBoxButtons.OK, MessageBoxIcon.Information)
Case 60 To 69
MessageBox.Show("You are in your sixties", "You are in your sixties", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Select
Me.Refresh()
TextBox1.Text = ""
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
Me.Button2.Text = "&How Old Are You?"
Me.Button1.Text = "&Exit"
End Sub
Re: [2005] String question..
For getting the integer input you could use a UpDownNumeric Control,which will control the input itself
Re: [2005] String question..
Double.TryParse attempts to parse a string representation of a double. Integer.TryParse attempts to parse a string representation of an integer. If you want to ensure that the user enters an integer then...
vb Code:
Dim myInteger As Integer
If Integer.TryParse(myString, myInteger) Then
'The operation was successful and the result is now contained in myInteger.
Else
'The operation not successful.
End If
I'm with danasegarane though, as you'll note from my first reply.
Re: [2005] String question..
Got it, thanks a ton jmcilhinney, and danasegarane. I removed agerange, and changed everything to myInteger, and made it look like this:
myInteger = Val(TextBox1.Text), the Val took away the error message when a string was imputted, and the tryparse gave an alert when it textbox1.text wasn't an integer.
Re: [2005] String question..
You don't use Val() at all. The whole point of TryParse is that it trys to parse the string to an integer and tells you whether it succeeded or not. Read my previous post again, particularly the comments. If the operation is successful then the Integer variable contains the parsed value.
Re: [2005] String question..