Office中国论坛/Access中国论坛

标题: 限制输入类型(类模块) [打印本页]

作者: andymark    时间: 2011-10-10 20:27
标题: 限制输入类型(类模块)
         限制输入类型的示例
  1. Private m_DataType As Integer   '定义数据类型
  2. Private WithEvents LimitTextBox As TextBox

  3. Enum DType
  4.     MyChar = 0     '字符类型
  5.     MyInt = 1      '整数
  6.     MyDecimal = 2  '小数
  7.     MyPhone = 3    '电话
  8.     MyEmail = 4    'Email
  9.     MyNone = 5     '常规
  10. End Enum


  11. Property Get DataType() As DType
  12.     DataType = m_DataType
  13. End Property

  14. Property Let DataType(Value As DType)
  15.     m_DataType = Value
  16. End Property

  17. Public Sub SetTextBoxType(LimitText As TextBox, LimitType As DType)
  18.   '设置文本数据类型
  19.     Set LimitTextBox = LimitText
  20.     With LimitTextBox
  21.         DataType = LimitType
  22.        .OnKeyPress = "[Event Procedure]"
  23.     End With
  24.   
  25. End Sub
  26. Private Sub LimitTextBox_KeyPress(KeyAscii As Integer)
  27.    
  28.    Select Case m_DataType
  29.       
  30.       Case 0
  31.          If Not IsChar(KeyAscii) = True Then KeyAscii = 0
  32.       Case 1
  33.           If Not IsInt(KeyAscii) = True Then KeyAscii = 0
  34.       Case 2
  35.           If Not IsDecimal(KeyAscii) = True Then KeyAscii = 0
  36.       Case 3
  37.            If Not IsPhone(KeyAscii) = True Then KeyAscii = 0
  38.       Case 4
  39.            If Not IsEmail(KeyAscii) = True Then KeyAscii = 0
  40.       Case 5
  41.          
  42.     End Select

  43. End Sub


  44. Private Sub Class_Initialize()
  45.     Set LimitTextBox = Nothing
  46. End Sub


  47. Private Function IsChar(ByVal A As Integer) As Boolean
  48.     If (A < 97 Or A > 122) And (A < 65 Or A > 90) And (A <> 8) And (A <> 32) Then
  49.         IsChar = False
  50.        Else
  51.         IsChar = True
  52.     End If
  53. End Function
  54. Private Function IsInt(ByVal A As Integer) As Boolean
  55.     If (A < 48 Or A > 57) And (A <> 8) Then
  56.       
  57.           IsInt = False
  58.        Else
  59.           IsInt = True
  60.     End If
  61. End Function
  62.    
  63. Private Function IsDecimal(ByVal KeyAscii As Integer) As Boolean
  64.     If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Or KeyAscii = Asc(".") Then
  65.         IsDecimal = True
  66.      Else
  67.         IsDecimal = False
  68.     End If
  69. End Function
  70. Private Function IsPhone(ByVal A As Integer) As Boolean
  71.     If (A < 48 Or A > 57) And (A <> 8) And (A <> Asc("-")) Then
  72.         IsPhone = False
  73.        Else
  74.         IsPhone = True
  75.     End If
  76. End Function
  77. Private Function IsEmail(ByVal A As Integer) As Boolean
  78.     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
  79.         IsEmail = False
  80.        Else
  81.         IsEmail = True
  82.     End If
  83. End Function
  84. Private Function IsProperDecimal(ByVal No As String) As Boolean
  85.     Dim NoLen
  86.     Dim DotFlag
  87.     DotFlag = 0

  88.     NoLen = Len(No)
  89.     Dim I As Integer
  90.     For I = 1 To NoLen
  91.         If Mid(No, I, 1) = "." Then DotFlag = DotFlag + 1
  92.     Next I
  93.     If DotFlag > 1 Then IsProperDecimal = False Else IsProperDecimal = True
  94. 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