I have a text box in VB which is set up to only accept only numeric data, and it works, except in TWO specific cases.
If the user provides a NON NUMERIC CHAR the text box self clears,
However, if the user first provides a number, then provides either '-' or '+'
The text box will accept this as a valid input.
When the user types one more char of ANY TYPE, i.e. number or char
Then the text box 'realises' and will self clear.
I was wondering if this is due to the way VB stores the chars '-' and '+'?
Is the best way around this to just add in the two exceptions, i.e. if '-' or '+' are input then self clear?
Or is there a more elegant solution?
Thank you.
Code:
Private Sub TextBox1_Change()
'Textval used as variable from user input
'Numval becomes textval providing the user input is numerical
Dim textval As String
Dim numval As String
textval = TextBox1.Text
If IsNumeric(textval) Then
numval = textval
Else
TextBox1.Text = CStr(numval)
End If
End Sub