Office中国论坛/Access中国论坛
标题:
限制输入类型(类模块)
[打印本页]
作者:
andymark
时间:
2011-10-10 20:27
标题:
限制输入类型(类模块)
限制输入类型的示例
Private m_DataType As Integer '定义数据类型
Private WithEvents LimitTextBox As TextBox
Enum DType
MyChar = 0 '字符类型
MyInt = 1 '整数
MyDecimal = 2 '小数
MyPhone = 3 '电话
MyEmail = 4 'Email
MyNone = 5 '常规
End Enum
Property Get DataType() As DType
DataType = m_DataType
End Property
Property Let DataType(Value As DType)
m_DataType = Value
End Property
Public Sub SetTextBoxType(LimitText As TextBox, LimitType As DType)
'设置文本数据类型
Set LimitTextBox = LimitText
With LimitTextBox
DataType = LimitType
.OnKeyPress = "[Event Procedure]"
End With
End Sub
Private Sub LimitTextBox_KeyPress(KeyAscii As Integer)
Select Case m_DataType
Case 0
If Not IsChar(KeyAscii) = True Then KeyAscii = 0
Case 1
If Not IsInt(KeyAscii) = True Then KeyAscii = 0
Case 2
If Not IsDecimal(KeyAscii) = True Then KeyAscii = 0
Case 3
If Not IsPhone(KeyAscii) = True Then KeyAscii = 0
Case 4
If Not IsEmail(KeyAscii) = True Then KeyAscii = 0
Case 5
End Select
End Sub
Private Sub Class_Initialize()
Set LimitTextBox = Nothing
End Sub
Private Function IsChar(ByVal A As Integer) As Boolean
If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A <> 32) Then
IsChar = False
Else
IsChar = True
End If
End Function
Private Function IsInt(ByVal A As Integer) As Boolean
If (A < 48 Or A > 57) And (A <> 8) Then
IsInt = False
Else
IsInt = True
End If
End Function
Private Function IsDecimal(ByVal KeyAscii As Integer) As Boolean
If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = Asc(".") Then
IsDecimal = True
Else
IsDecimal = False
End If
End Function
Private Function IsPhone(ByVal A As Integer) As Boolean
If (A < 48 Or A > 57) And (A <> 8) And (A <> Asc("-")) Then
IsPhone = False
Else
IsPhone = True
End If
End Function
Private Function IsEmail(ByVal A As Integer) As Boolean
If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A < 48 Or A > 57) And A <> Asc("-") And A <> Asc("@") And A <> Asc(".") Then
IsEmail = False
Else
IsEmail = True
End If
End Function
Private Function IsProperDecimal(ByVal No As String) As Boolean
Dim NoLen
Dim DotFlag
DotFlag = 0
NoLen = Len(No)
Dim I As Integer
For I = 1 To NoLen
If Mid(No, I, 1) = "." Then DotFlag = DotFlag + 1
Next I
If DotFlag > 1 Then IsProperDecimal = False Else IsProperDecimal = True
End Function
复制代码
完整示例
[attach]46907[/attach]
作者:
tanhong
时间:
2011-10-10 20:43
有些日了没见到老兄新作了,收藏了,多谢分享。
作者:
Henry D. Sy
时间:
2011-10-10 22:20
今天大请客
作者:
xuwenning
时间:
2011-10-11 09:00
谢谢分享
收下
作者:
yanghua1900363
时间:
2011-10-11 11:30
收藏了 谢谢分享
作者:
yanwei82123300
时间:
2011-10-12 08:15
多谢分享。
作者:
koutx
时间:
2011-10-12 11:58
好东西一定要收藏
作者:
huangli0356
时间:
2012-11-14 09:21
谢谢分享
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3