'控制KeyPress事件
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = F_iKeyCheck(KeyAscii, Text1.MaxLength)
'KeyAscii = F_iKeyCheck(KeyAscii, 30) 也行
End Sub
Private Function F_iKeyCheck(ByVal iKeyAscii As Integer, Optional ByVal iLength As Integer = 0) As Integer
Dim iRet As Integer
Dim i As Integer
Dim S As String
'如果对输入的长度没有限制,就取Text1的长度限制。
If iLength = 0 Then iLength = Text1.MaxLength
S = Text1.Text
S = StrConv(S, vbFromUnicode)
If LenB(S) - Text1.SelLength + LenB(StrConv(Chr(iKeyAscii), vbFromUnicode)) > iLength Then
iRet = 0
End If
F_iKeyCheck = iRet