在TEXT文本框中过滤输入为数值
参数keyin为输入值,txt为检查数值字符串,editable为是否backspace是否可用,xsw 为小数的位数,position 光标位置
'调用举例
'Private Sub Text1_KeyPress(KeyAscii As Integer)
' KeyAscii = FilterNum(KeyAscii, Text1.Text, True,2,Text1.selstart)
'End Sub
Public Function FilterNum(ByVal KeyIn As Integer, ByVal Txt As String, ByVal EditAble As Boolean, ByVal XSW As Integer, ByVal Position As Integer) As Integer
Dim ValidateList As String
Dim ValidateString As String
Dim KeyOut As Integer
Dim i As Integer
ValidateString = "-0123456789."
If EditAble = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
FilterNum = 0
Exit Function
End If
If KeyOut = 8 Then
FilterNum = 8
Exit Function
End If
If (KeyOut = Asc("-")) And Not (Position = 0 And Left(Txt, 1) <> "-") Then
FilterNum = 0
Exit Function
End If
If XSW > 0 Then
i = InStr(1, Txt, ".", vbTextCompare)
If i > 0 And KeyOut = Asc(".") Then
FilterNum = 0
Exit Function
End If
If i > 0 And i + XSW = Len(Txt) Then
If KeyOut <> Asc("-") Then
FilterNum = 0
End If
End If
If i <= 0 Then
If Left(Txt, 1) = "-" And KeyOut = Asc(".") And Position = 1 Then
FilterNum = 0
Exit Function
End If
End If
Else
If KeyOut = Asc(".") Then
FilterNum = 0
Exit Function
End If
End If
FilterNum = KeyOut
End Function
浙公网安备 33010602011771号