TextBox 限制只能输入数字
来源:老怪
Private Sub Text1_KeyPress(KeyAscii As Integer)
'KeyAscii 32 以下是一些控制键,拦接会造成操作障碍
If KeyAscii >= 33 Then
If KeyAscii <= vbKey9 And KeyAscii>= vbKey0 Then
Else
'把 KeyAscii 設為 0 就是取消輸入
KeyAscii = 0
MsgBox "不可輸入非數字字元"
End If
End If
End Sub
解說:
KeyAscii 之键码同於 KeyCode 之鍵碼,你可以查 VB 說明之 KeyCode 一項,
但有些Keycode 鍵碼一定要在 KeyDown 和 KeyUp 裏才收的到,在 KeyPress 的 KeyAscii裏收不到。
利用 KeyAscii =0 的方式在 KeyPress 裏有用,在 KeyDown 裏把 KeyCode=0 可就不管用了,
所以 Down,Press,Up 各有各的用處,要分清楚才好。