Below is the code I implemented when using VB6.0
[Vb]
Private Sub txtCash_KeyPress (KeyAscii As Integer) 'limits the input amount to only numbers
If KeyAscii <48 Or KeyAscii> 57 Then
KeyAscii = 0
MsgBox "only numbers can be entered !, Enter again! ", VbOKOnly + vbExclamation," warning"
TxtCash. SetFocus
End If
End Sub
Private Sub txtCash_KeyPress (KeyAscii As Integer) 'limits the input amount to only numbers
If KeyAscii <48 Or KeyAscii> 57 Then
KeyAscii = 0
MsgBox "only numbers can be entered !, Enter again! ", VbOKOnly + vbExclamation," warning"
TxtCash. SetFocus
End If
End Sub
The following two methods can be used to solve this problem using VB. NET:
1. After the input, the system determines
After the input is controlled, all the data in the text box is input to determine whether all the entered data is numbers. The Code is as follows:
[Csharp]
If IsNumeric (txtCash. Text) = False Then
'Msgbox ("Please enter a valid number value in the amount ")
'Txtcash. Focus ()
'Txtcash. SelectAll ()
'Exit Sub
'End If
If IsNumeric (txtCash. Text) = False Then
'Msgbox ("Please enter a valid number value in the amount ")
'Txtcash. Focus ()
'Txtcash. SelectAll ()
'Exit Sub
'End If, but I think it will take a lot of time, because it is only known after the program runs, rather than knowing that the input is wrong.
2. Input judgment
I used this method, and I can judge it when I try again. The Code is as follows:
[Csharp]
Private Sub txtCash_KeyPress (sender As Object, e As KeyPressEventArgs) Handles txtCash. KeyPress
If Char. IsNumber (e. KeyChar) Or e. KeyChar = Chr (Keys. Back) then' should be allowed.
Return
Else
MsgBox ("enter a number ")
E. Handled = False
End If
E. Handled = True
End Sub
Private Sub txtCash_KeyPress (sender As Object, e As KeyPressEventArgs) Handles txtCash. KeyPress
If Char. IsNumber (e. KeyChar) Or e. KeyChar = Chr (Keys. Back) then' should be allowed.
Return
Else
MsgBox ("enter a number ")
E. Handled = False
End If
E. Handled = True
End Sub
E. keyChar is used to obtain the character corresponding to the key pressed by the keyboard, and keys. Back is the return key command.
E. Handled returns a boolean to indicate whether the event has been processed.