I did some research on this and still cannot get my program to work. I just need to check the text box to see if user input is a numeric value or not (except for "." And "/")
My code is still
Private Sub Num1_KeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Num1.KeyPress Dim UserEntry As Boolean If UserEntry = IsNumeric(False) Then MessageBox.Show("That not numeric!") End If End Sub
I think you better use the event TextBox.KeyUpit passes KeyEventArgs. Try the following:
TextBox.KeyUp
KeyEventArgs
Private Sub Num1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles Num1.KeyUp Dim isDigit As Boolean = Char.IsDigit(ChrW(e.KeyValue)) Dim isKeypadNum As Boolean = e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9 Dim isBackOrSlashOrPeriod As Boolean = (e.KeyCode = Keys.Decimal Or e.KeyCode = Keys.Oem2 Or e.KeyCode = Keys.Back Or e.KeyCode = Keys.OemPeriod) If Not (isDigit Or isKeypadNum Or isBackOrSlashOrPeriod) Then MessageBox.Show("That not numeric!") End If End Sub
I recommend handling TextChanged and checking for an integer instead of a single character.
Private Sub Num1_TextChanged(sender As Object, e As TextChangedEventArgs) Handles Num1.TextChanged If IsInputNumeric(Num1.Text) Then 'handle numeric input Else 'handle not a number End If End Sub Private Function IsInputNumeric(input As String) As Boolean If String.IsNullOrWhiteSpace(input) Then Return False If IsNumeric(input) Then Return True Dim parts() As String = input.Split("/"c) If parts.Length <> 2 Then Return False Return IsNumeric(parts(0)) AndAlso IsNumeric(parts(1)) End Function
Public Function onlyNumbers(ByVal KeyChar As Char) As Boolean Dim allowedChars As String allowedChars = "0123456789./" If allowedChars.IndexOf(KeyChar) = -1 And (Asc(KeyChar)) <> 8 Then Return True End If Return False End Function
true char.
:
e.handled = onlyNumbers(e.keychar)
, textBox LostFocus eventHandler, , . OK.
LostFocus
a) - "0123456789./" ,
"0123456789./"
b) , "/" ( ), IsNumeric() . - , .
"/"
IsNumeric()
, 1/2/2,.i.e. 1/4. , , 1 "/".
Private Sub tbYear_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles tbYear.KeyPress If e.KeyChar < Chr(48) Or e.KeyChar > Chr(57) Then e.KeyChar = Nothing End If End Sub
' Validates textboxes for numeric only keystrokes. Hook this up to the ' PreviewTextInput of the desired textbox Private Sub SetTextboxNumericOnly(sender As Object, e As TextCompositionEventArgs) Dim regex As New System.Text.RegularExpressions.Regex("[^0-9]+") e.Handled = regex.IsMatch(e.Text) End Sub
, , , . , .
, , , .TryParse, , IsNumeric:
, TryParse , IsNumeric .The second reason is that you can provide IsNumeric with any object you want (also, for example, Button), and it accepts it. You would never find such errors at compile time. Instead, with TryParse, you can only pass a string as your first parameter.
, TryParse , IsNumeric .
The second reason is that you can provide IsNumeric with any object you want (also, for example, Button), and it accepts it. You would never find such errors at compile time. Instead, with TryParse, you can only pass a string as your first parameter.