Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((Asc("") < 49 And Asc("") > 57) Or (Asc("") <> 46) Or (Asc("") <> 118)) Then Text1.Text = Text1.Text & ""
End If
End Sub
你可以这样写:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 49) And (KeyAscii <= 57)) Then '不在 1 ~ 9 范围 KeyAscii = 0 '这样就可以不显示输入的字符了 Beep'提示用户输入不正确,发出"嘀"声
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not ((KeyAscii >= 49) And (KeyAscii <= 57)) And KeyAscii <> vbKeyBack Then '不在 1 ~ 9 和退格键范围 KeyAscii = 0 '这样就可以不显示输入的字符了 Beep'提示用户输入不正确,发出"嘀"声
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("0") To Asc("9"), vbKeyBack '允许 0~9数字和退格键
Case Asc(".") '只允许一个小数点
If InStr(1, Text1.Text, ".") > 0 Then KeyAscii = 0
Case Else '其他键无效并发出Beep声音
KeyAscii = 0
Beep
End Select
End Sub