Private Sub txtNum_KeyPress(Index As Integer, KeyAscii As Integer) If (KeyAscii <> vbKeyDelete) And (KeyAscii <> vbKeyBack) And (KeyAscii <> 13) And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End Ifend sub
KeyAscii = 0
End Ifend sub
etDefault = 0 'Valid
etInvalid = 1 'Invalid
etRange = 2 'input range
etMaxLength = 3 'maximum length
etEmpty = 4 'empty
etModified = 5 'auto correct
etUnknown = 99 'unknow error
End Enum'input type
Public Enum enumInputType
ITNUMERIC = 0 'number
itChar = 1 'char
itDate = 2 'date
itTime = 3 'time
itID = 4 'id
End EnumPublic Enum enumDateTime
dtDate = 0 'date
dtTime = 1 'time
End EnumPublic Function FilterInput(ByRef objInputBox As Object, _
ByVal intKeyAscii As Integer, _
Optional ByVal udtInputType As enumInputType = ITNUMERIC, _
Optional ByVal strKeyCodeRange As String = "0-9", _
Optional ByVal blnSmallNumber As Boolean = False, _
Optional ByVal intMaxLength As Integer = 0, _
Optional ByVal blnShowMsg As Boolean = False _
) As Integer
'************************************************************************
'*
'* ◇Function:FilterInput
'* work:limit input, filter illegal chars,
'* support number,text, date, time etc
'* para:objInputBox ---- 输入框对象(支持 TextBox、ComboBox 等)
'* intKeyAscii ---- 键盘输入码:对应keypress中的keyascii
'* strKeyCodeRange(可选)---- "字的范围,比如:0-9"
'* blnSmallNumber(可选) ---- 允许(".")
'* intMaxLength(可选) ---- 最大长度
'* blnShowMsg(可选) ---- 显示帮助信息
'* return:[Integer]如果输入有效刚返回intkeyascii,否则返回0
'*
'* notation:range char"-", split char"|"
'* sample : "a-z|.|0-9|,|A-Z|~"
'*
'* Last Modified by:Unruled Boy @ 1/12/2002
'*
'************************************************************************* Dim o_strRet() As String
Dim o_strText As String
Dim o_strRange As String
Dim o_intItems As Integer
Dim o_intRet As Integer
Dim o_intKeyAscii As Integer
'Dim o_blnRet As Boolean
Dim o_udtErrorType As enumErrorType
o_udtErrorType = etDefault 'initial error:none
With objInputBox
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
'if there is a maximum length, use it
If .MaxLength > 0 Then '
If intMaxLength = 0 Then
intMaxLength = .MaxLength
Else
If intMaxLength > .MaxLength Then
intMaxLength = .MaxLength
Else
End If
End If
Else
End If
Case Else
End Select
o_strText = .Text
If Chr(intKeyAscii) = "." And InStr(o_strText, ".") > 0 And udtInputType = ITNUMERIC Then GoTo handleError
End With
'if the current length exceeds the maximum length,
'show hint
If intMaxLength > 0 And Len(o_strText) >= intMaxLength Then
Select Case intKeyAscii
Case vbKeyDelete, vbKeyBack 'Delete & Backspace
o_intKeyAscii = intKeyAscii
Case Else
Select Case TypeName(objInputBox)
Case "TextBox", "RichTextBox"
'if the user select a range of text
If objInputBox.SelLength > 0 Then
o_intKeyAscii = intKeyAscii
Else
Beep
o_udtErrorType = etMaxLength
o_intKeyAscii = 0
End If
Case Else
Beep
o_udtErrorType = etMaxLength
o_intKeyAscii = 0
End Select
End Select
Else
'first, we process those speical keys
Select Case intKeyAscii
Case vbKeyDelete, vbKeyBack 'as above
o_intKeyAscii = intKeyAscii
Case vbKeyDecimal, 190 'dot
If udtInputType = ITNUMERIC And blnSmallNumber Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case vbKeySubtract, 47, 45 'date time chars:-、/ 'vbKeyDivide应该是47而microsoft定义为111 bug
If udtInputType = itDate Then
o_intKeyAscii = intKeyAscii
ElseIf udtInputType = ITNUMERIC Then
If Len(o_strText) = 0 Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case Asc(":") 'time char(:)
If udtInputType = itTime Then
o_intKeyAscii = intKeyAscii
Else
o_udtErrorType = etInvalid
o_intKeyAscii = 0
End If
Case Else
'o_blnRet = False
o_strRange = strKeyCodeRange
If o_strRange <> vbNullString Then
o_intItems = 0
'trim the redundant split chars
Do While o_intItems < Len(o_strRange)
If Left(o_strRange, 1) = "|" Then
o_strRange = Right(o_strRange, _
Len(o_strRange) - 1)
ElseIf Right(o_strRange, 1) = "|" Then
o_strRange = Left(o_strRange, _
Len(o_strRange) - 1)
Else
End If
o_intItems = o_intItems + 1
Loop
'explain the combined para
o_strRet() = Split(o_strRange, "|")
For o_intItems = LBound(o_strRet) To UBound(o_strRet)
o_intRet = InStr(o_strRet(o_intItems), "-")
If o_intRet <> 0 Then
If intKeyAscii >= Asc(Left(o_strRet(o_intItems), o_intRet - 1)) _
And intKeyAscii <= Asc(Right(o_strRet(o_intItems), Len(o_strRet(o_intItems)) - o_intRet)) Then
o_intKeyAscii = intKeyAscii
Exit For
End If
Else
If intKeyAscii = Val(o_strRet(o_intItems)) Then
'o_blnRet = True
o_intKeyAscii = intKeyAscii
Exit For
Else
End If
End If
Next
If o_intKeyAscii = 0 Then
o_udtErrorType = etRange
Else
End If
Else
o_intKeyAscii = intKeyAscii
End If End Select
End If
'if there is something wrong, give hint
If o_udtErrorType <> etDefault Then
If blnShowMsg Then
Select Case o_udtErrorType
Case etRange
MsgBox "Your input is not in the [range]." & _
"Valid input range is:" & _
Replace(o_strRange, "|", ",") & ".", _
vbInformation
Case etMaxLength
MsgBox "Your input exceeds the [maximum length]." & _
"The maximum length is: " & _
CStr(intMaxLength), vbInformation
Case etInvalid
MsgBox "Your input is invalid, please check it." & _
"Hint:valid input are:" & _
Replace(o_strRange, "|", ",") & ".", _
vbInformation
Case Else
End Select
Else
End If
Else
End If
FilterInput = o_intKeyAscii
Exit Function
handleError:
FilterInput = 0
If blnShowMsg Then
MsgBox "Error occurs when filtering the input:" & vbCrLf _
& Err.Description
Else
End If
On Error GoTo 0
End Function
if not isNumeric(text1) then msgbox "please enter a number"
lngStyle=lngStyle OR EN_NUMBER
SetWindowLong(Text1.Hwnd,GWL_STYLE, lngStyle)
Private Sub TextBox_KeyPress(Index As Integer, KeyAscii As Integer)
If Index = 3 Or Index = 4 Then
Else
If InStr("0123456789" + Chr(13) + Chr(8), Chr(KeyAscii)) = 0 Then KeyAscii = 0
End If
If KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{TAB}"
End If
End Sub