Private Sub aaa_KeyPress(KeyAscii As Integer) KeyAscii = OnlyInt(KeyAscii) End Sub'只能输入INT Public Function OnlyInt(KeyAscii As Integer) As String OnlyInt = ValiText(KeyAscii, "0123456789", True) End Function
'在FORM1中放个TEXT1,设置其text为"" Private Sub Form_Load() Form1.KeyPreview = True End SubPrivate Sub Text1_Change() If IsNumeric(Text1) = False Then Text1 = "错误,只可以是数字" End Sub '但似乎还不如用KEYASCII来判断简单
private sub Text1_KeyPress(keyascii as integer) if isnumeric(text1)=false then keyascii=0end sub
IsNumeric Returns a Boolean value indicating whether an expression can be evaluated as a number.Syntax IsNumeric(expression)The expression argument can be any expression.Res IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.IsNumeric returns False if expression is a date expression.
private sub txt_keypress(keyascii as integer)if keyascii>9 or keyascii<0 then msgbox "请正确输入数字!" txt.setfocus txt.text="" end sub
keyascii很好用啊?但要记住添进Enter和Back space,要不然很麻烦的。
Public Function sffunLimitNumber(ByVal IntVal As Integer) As Integer '-------------------1------------------- '目 的:只允许在文本框内输入数字、退格、删除及回车键 '输 入:ByVal IntVal As Integer,任意的键值 '被传递值:无 '返 回 值:过滤后的键值 '输 出:无 '注 解: '用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可 '修 订 版: '-------------------1------------------- If (IntVal <> vbKeyDelete) _ And (IntVal <> vbKeyBack) _ And (IntVal <> 13) _ And (IntVal < 48 Or IntVal > 57) Then IntVal = 0 End If sffunLimitNumber = IntValEnd Function
KeyAscii = OnlyInt(KeyAscii)
End Sub'只能输入INT
Public Function OnlyInt(KeyAscii As Integer) As String
OnlyInt = ValiText(KeyAscii, "0123456789", True)
End Function
Private Sub Form_Load()
Form1.KeyPreview = True
End SubPrivate Sub Text1_Change()
If IsNumeric(Text1) = False Then Text1 = "错误,只可以是数字"
End Sub
'但似乎还不如用KEYASCII来判断简单
Returns a Boolean value indicating whether an expression can be evaluated as a number.Syntax
IsNumeric(expression)The expression argument can be any expression.Res
IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.IsNumeric returns False if expression is a date expression.
msgbox "请正确输入数字!"
txt.setfocus
txt.text=""
end sub
'-------------------1-------------------
'目 的:只允许在文本框内输入数字、退格、删除及回车键
'输 入:ByVal IntVal As Integer,任意的键值
'被传递值:无
'返 回 值:过滤后的键值
'输 出:无
'注 解:
'用 法:在文本框的KeyPress事件中输入KeyAscii = sffunLimitNumber(KeyAscii)即可
'修 订 版:
'-------------------1-------------------
If (IntVal <> vbKeyDelete) _
And (IntVal <> vbKeyBack) _
And (IntVal <> 13) _
And (IntVal < 48 Or IntVal > 57) Then
IntVal = 0
End If
sffunLimitNumber = IntValEnd Function
mask 为#######,数目为你要输入的最大字符数
promptchar为空格
不过我觉得不怎么好用
KeyAscii = 0
End If
If Val(text1.text) & "" = text1.text Then
MsgBox "是数字"
else
MsgBox "不是数字"
End If