Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii > Asc("9") Or KeyAscii < Asc("0")) And KeyAscii <> Asc(".") And KeyAscii <> 45 And KeyAscii <> 8 Then KeyAscii = 0
End Sub
'这种方法有缺陷,用户可以输入33..33,建议你使用下面的方法
Private Sub Text1_LostFocus() '文本框失去焦点
If IsNumeric(Text1.Text) = False Then
MsgBox "请输入数字", vbExclamation
ElseIf Trim(Text1.Text) = "" Then
MsgBox "文本框不能为空", vbExclamation
Else
Exit Sub
End If
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
直接在TEXT的KeyPress事件里进行判断
If Not IsNumeric(Chr(KeyAscii)) And KeyAscii <> 8 Then KeyAscii = 0