In interface programming, sometimes we only want the TextBox control to receive numeric input, while other inputs are ignored. How to achieve this? The specific source code is given below:
We add KeyPress event for TextBox
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii <> 8 And KeyAscii < 48 Or KeyAscii > 57 Then
Beep
KeyAscii = 0
End If
End Sub
The specific principle above is: determine whether the KeyAscii value of the pressed key is between 48-57. If it is between 48 and 57, the input is a number, otherwise it is not. However, sometimes we may also use the back key to facilitate input. Delete on error, in this case, so KeyAscii <> 8 must be added so that it is not ignored when using the back key.