comparing number to string

RonMex

Member
Joined
Apr 9, 2011
Messages
10
Programming Experience
3-5
i am trying to compare user input number to with strings. i want the users to only input numeric values in a text box. but if they input a string i want an error message to go inform then to input numbers. this is the code i have:

'asigning values to text boxes
variable1 = Var1.Text

Do While IsNumeric(variable1) = False And IsNumeric(variable2) = False
variable2 = Var2.Text
If IsNumeric(variable1) Or IsNumeric(variable2) = False Then
MsgBox("please enter a numeric value")
End If
Loop

it does not work(this is for a calculator)
 
Why are you using a loop?

Anyways, check out the Decimal.TryParse() function:
VB.NET:
Dim Variable1 As Decimal
If Var1.Text.Trim.Length = 0I Not Decimal.TryParse(Var1.Text.Trim, Variable1) Then
    'Display an error, the input isnt numeric
Else
    'Variable1 holds the value now
End If
 
Why are you using a loop?

Anyways, check out the Decimal.TryParse() function:
VB.NET:
Dim Variable1 As Decimal
If Var1.Text.Trim.Length = 0I Not Decimal.TryParse(Var1.Text.Trim, Variable1) Then
    'Display an error, the input isnt numeric
Else
    'Variable1 holds the value now
End If

i want it to display the error messgae until a numberic value is input. what is OI
 
Restrict textbox input to digits only.

Place the following code in your textbox's Keypress even to allow only digits to be input. It will also allow a single decimal point, the backspace key to delete a character, and the Enter key to continue on to another control.

VB.NET:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
		If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
		If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False
		If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
		If e.KeyChar = Chr(13) Then TextBox2.Focus() 'Enter key moves to specified control
	End Sub
 
i want it to display the error messgae until a numberic value is input. what is OI
Right, which that certainly wont happen if you use a loop like that because the form will be unresponsive while you're looping looking for valid input. The user would be stuck clicking the messagebox away too often to give it better input.

What you need to be doing is notifying the user when they click the button, or the text changes or whenever their input needs to be validated.

Or better yet, use a control that's actually meant to be used for gathering numeric only input from users, like the NumericUpDown control.
 
Place the following code in your textbox's Keypress even to allow only digits to be input. It will also allow a single decimal point, the backspace key to delete a character, and the Enter key to continue on to another control.

VB.NET:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
        If e.KeyChar = "." And TextBox1.Text.IndexOf(".") = -1 Then e.Handled = False
        If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
        If e.KeyChar = Chr(13) Then TextBox2.Focus() 'Enter key moves to specified control
    End Sub

thank you for the different approach
 
thank you for the different approach
One thing you should be aware of with this approach is that users can paste data into the TextBox and that KeyPress event wont fire. It's things like that to why I like the NumericUpDown control since it's built to handle getting numeric input from the user.

Another thing to watch out for is anytime the Text gets changed, the above code wont account for either.
 
Add a TextChanged event to assure correct digital values in textbox.

In case a change was made elsewhere in your program, add a TextChanged event in addition to the KeyPress event:

VB.NET:
	Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
		For Each ch As Char In TextBox1.Text
			If Not Char.IsDigit(ch) Then TextBox1.Clear()
		Next
	End Sub
 
In case a change was made elsewhere in your program, add a TextChanged event in addition to the KeyPress event:

VB.NET:
	Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
		For Each ch As Char In TextBox1.Text
			If Not Char.IsDigit(ch) Then TextBox1.Clear()
		Next
	End Sub
See, now you're just hacking things together. What if it's a number with a decimal? Or a comma? or a negative number? What about non-US numbers? Imagine a control that already handles all of that, imagine a control that does it and you wouldn't have to worry about maintaining, also imagine this control being a component of the FW itself too. I swear, not enough people utilize the NumericUpDown for numeric input.

Also, rather than handling the TextChanged event which will fire with every key press for every valid character, I would override the WndProc sub and check for the paste message.
VB.NET:
    Private Const WM_PASTE As Integer = &H302

    Protected Overloads Overrides Sub WndProc(ByRef m As Message)
        If m.Msg = WM_PASTE Then
            'Code for paste
        End If
        MyBase.WndProc(m)
    End Sub
 
Back
Top