给你一个函数: '过滤输入为数值 '参数keyin为输入值,txt为检查数值字符串,editable为是否backspace是否可用,xsw 为小数的位数,position 光标位置 '调用举例 'Private Sub Text1_KeyPress(KeyAscii As Integer) ' KeyAscii = FilterNum(KeyAscii, Text1.Text, True,Text1.tag,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 '过滤输入文本 '参数keyin为输入值,validatestring为过滤字符串,editable为是否backspace是否可用。 '调用举例 'Private Sub Text1_KeyPress(KeyAscii As In teger) ' KeyAscii = FilterNum(KeyAscii, "abc", True)'只可以输入abc 'End Sub Public Function Filtertext(KeyIn As Integer, ValidateString As String, EditAble As Boolean) As Integer Dim ValidateList As String Dim KeyOut As Integer 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 KeyOut = 0 End If Filtertext = KeyOut End Function
◆第一步:创建窗体Form1 添加控件:一个TextBox控件,命名为txtTest;一个Frame控件,命名为frameStyles;在其上分别添加三个OptionoptButton控件,命名为optStyle,索引分别为0、1与2。 ◆第二步:添加代码‘主程序: Option Explicit Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Const GWL_STYLE = (-16)Enum TextInputStyles ES_UPPERCASE = &H8& ES_LOWERCASE = &H10& ES_NUMBER = &H2000& End EnumPublic Sub SetTextInputStyle(hWndTextControl As Long, InputStyle As TextInputStyles) Dim Style As Long Style = GetWindowLong(hWndTextControl, GWL_STYLE) Style = Style Or InputStyle SetWindowLong hWndTextControl, GWL_STYLE, Style End SubPrivate Sub optStyle_Click(Index As Integer) Select Case Index Case 0: SetTextInputStyle txtTest.hWnd, ES_UPPERCASE Case 1: SetTextInputStyle txtTest.hWnd, ES_LOWERCASE Case 2: SetTextInputStyle txtTest.hWnd, ES_NUMBER End Select End Sub
'过滤输入为数值
'参数keyin为输入值,txt为检查数值字符串,editable为是否backspace是否可用,xsw 为小数的位数,position 光标位置
'调用举例
'Private Sub Text1_KeyPress(KeyAscii As Integer)
' KeyAscii = FilterNum(KeyAscii, Text1.Text, True,Text1.tag,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
'过滤输入文本
'参数keyin为输入值,validatestring为过滤字符串,editable为是否backspace是否可用。
'调用举例
'Private Sub Text1_KeyPress(KeyAscii As In teger)
' KeyAscii = FilterNum(KeyAscii, "abc", True)'只可以输入abc
'End Sub
Public Function Filtertext(KeyIn As Integer, ValidateString As String, EditAble As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
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
KeyOut = 0
End If
Filtertext = KeyOut
End Function
◆第一步:创建窗体Form1
添加控件:一个TextBox控件,命名为txtTest;一个Frame控件,命名为frameStyles;在其上分别添加三个OptionoptButton控件,命名为optStyle,索引分别为0、1与2。
◆第二步:添加代码‘主程序:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As LongPrivate Const GWL_STYLE = (-16)Enum TextInputStyles
ES_UPPERCASE = &H8&
ES_LOWERCASE = &H10&
ES_NUMBER = &H2000&
End EnumPublic Sub SetTextInputStyle(hWndTextControl As Long, InputStyle As TextInputStyles)
Dim Style As Long
Style = GetWindowLong(hWndTextControl, GWL_STYLE)
Style = Style Or InputStyle
SetWindowLong hWndTextControl, GWL_STYLE, Style
End SubPrivate Sub optStyle_Click(Index As Integer)
Select Case Index
Case 0: SetTextInputStyle txtTest.hWnd, ES_UPPERCASE
Case 1: SetTextInputStyle txtTest.hWnd, ES_LOWERCASE
Case 2: SetTextInputStyle txtTest.hWnd, ES_NUMBER
End Select
End Sub