by Masino Sinaga (7 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: Visual Basic 5.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
Many tips (and trick) tell us that if we want the textbox control ignore the character which is not numeric character, then we can just put the code that shown in KeyPress event procedure below. But, sometimes we forgot that although we have put the code in KeyPress event procedure, user still can input the alpha character to textbox control by doing copy and Paste to textbox. So, here is another tips to fix the problem. I hope this helpful.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") & Chr(13) _
And KeyAscii <= Asc("9") & Chr(13) _
Or KeyAscii = vbKeyBack _
Or KeyAscii = vbKeyDelete _
Or KeyAscii = vbKeySpace) Then
Beep
KeyAscii = 0
End If
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") & Chr(13) _
And KeyAscii <= Asc("9") & Chr(13) _
Or KeyAscii = vbKeyBack _
Or KeyAscii = vbKeyDelete _
Or KeyAscii = vbKeySpace) Then
Beep
KeyAscii = 0
End If
End Sub
'If user paste the character which is not
'numeric character, Text1 will ignore it.
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
Text1.Text = ""
End If
End Sub
'Try Paste some character which is not numeric
'to Text1 and Text2 control (copy alpha character
'from another file, paste it to those textboxes).
'See the difference between Text1 and Text2!!!
'So, don't forget to add the code in event
'procedure Change belongs to the textbox if
'you want your textbox control avoid the character
'which is not numeric. This is often we forgot!
No comments have been posted about A Must See Tips: Real Numeric Character in TextBox. Avoid Paste Alpha Character. Why not be the first to post a comment about A Must See Tips: Real Numeric Character in TextBox. Avoid Paste Alpha Character.