VB文本框限制输入类型

前端之家收集整理的这篇文章主要介绍了VB文本框限制输入类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VB文本框可以利用Ascii字符来限定输入的内容类型,这里我分了两大部分来阐述怎样限制数据类型,其实原理都是一样的,都是Ascii字符范围设定的问题,大家可以根据Ascii表来限制数据类型。


一、私有过程,不可调用


1、限定只输入汉字

代码

  1. Private Sub txtDirector_KeyPress(KeyAscii As Integer)
  2. If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
  3. Else
  4. KeyAscii = 0
  5. MsgBox "请输入汉字!",vbOKOnly + vbExclamation,"警告"
  6. txtDirector.SetFocus
  7. End If
  8. End Sub



这条语句用来判断输入的字符是否是汉字,如果不是汉字,就把这个输入的字符屏蔽掉。
keyAscii=0的字符是“空格”,keyAscii=8的字符是“退格”

2、限定只输入数字


查ASCII码表,得到0的ASCII码是48。输入以下语句:

  1. If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0



这条语句用来判断输入的字符是否在0-9的范围,如果不在这个范围,就把这个输入的字符屏蔽掉。(。其他的地方和上面的代码原理都是一样的 。


如果输入的数字可能是小数,那么还要添加如下代码
If KeyAscii = 46 And Not CBool(InStr(txbNumber,".")) Then Exit Sub

当输入小数点时,程序判断文本框中是否已有小数点(因为一个小数中不可能有多个小数点),如果没有小数点,则允许输入。

二、公有过程,在模块中可调用

代码

  1. Option Explicit
  2. Public Enum FormatType
  3. [数字_正整数] = 0
  4. [数字_正负整数] = 1
  5. [数字_正小数] = 2
  6. [数字_正负小数] = 3
  7. [字母_任意书写] = 4
  8. [字母_锁定小写] = 5
  9. [字母_锁定大写] = 6
  10. [汉字_锁定中文] = 7
  11. End Enum
  12.  
  13.  
  14. Function TextFormat(txtObj As TextBox,KeyAscii As Integer,Optional FormatType As FormatType = 0) As Integer
  15. Dim ReturnKeyCode As Integer
  16. Select Case FormatType
  17. Case 0
  18. ReturnKeyCode = TextFormat_0(txtObj,KeyAscii)
  19. Case 1
  20. ReturnKeyCode = TextFormat_1(txtObj,KeyAscii)
  21. Case 2
  22. ReturnKeyCode = TextFormat_2(txtObj,KeyAscii)
  23. Case 3
  24. ReturnKeyCode = TextFormat_3(txtObj,KeyAscii)
  25. Case 4
  26. ReturnKeyCode = TextFormat_4(txtObj,KeyAscii)
  27. Case 5
  28. ReturnKeyCode = TextFormat_5(txtObj,KeyAscii)
  29. Case 6
  30. ReturnKeyCode = TextFormat_6(txtObj,KeyAscii)
  31. Case 7
  32. ReturnKeyCode = TextFormat_7(txtObj,KeyAscii)
  33. End Select
  34. TextFormat = ReturnKeyCode
  35. End Function
  36. '=====================================================
  37. '数字_正整数
  38. '=====================================================
  39. Private Function TextFormat_0(txtObj As TextBox,KeyAscii As Integer) As Integer
  40. If KeyAscii = 8 Then TextFormat_0 = KeyAscii: Exit Function
  41. If KeyAscii = Asc("0") And Len(txtObj.Text) = 0 Then TextFormat_0 = 0: Exit Function
  42. If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  43. TextFormat_0 = 0
  44. Exit Function
  45. End If
  46. TextFormat_0 = KeyAscii
  47. End Function
  48. '=====================================================
  49. '数字_正负整数
  50. '=====================================================
  51. Private Function TextFormat_1(txtObj As TextBox,KeyAscii As Integer) As Integer
  52. If KeyAscii = 8 Then TextFormat_1 = KeyAscii: Exit Function
  53. If KeyAscii = Asc("0") And Len(txtObj.Text) = 0 Then TextFormat_1 = 0: Exit Function
  54. If KeyAscii = Asc("-") And Len(txtObj.Text) > 0 Then TextFormat_1 = 0: Exit Function
  55. If KeyAscii = Asc("-") And Len(txtObj.Text) = 0 Then TextFormat_1 = KeyAscii: Exit Function
  56. If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  57. TextFormat_1 = 0
  58. Exit Function
  59. End If
  60. TextFormat_1 = KeyAscii
  61. End Function
  62. '=====================================================
  63. '数字_正小数
  64. '=====================================================
  65. Private Function TextFormat_2(txtObj As TextBox,KeyAscii As Integer) As Integer
  66. If KeyAscii = 8 Then TextFormat_2 = KeyAscii: Exit Function
  67. If KeyAscii = Asc(".") And InStr(1,txtObj.Text,".") > 0 Then TextFormat_2 = 0: Exit Function
  68. If (KeyAscii = Asc(".") Or KeyAscii = Asc("0")) And Len(txtObj.Text) = 0 Then txtObj.Text = "0.": txtObj.SelStart = Len(txtObj.Text): TextFormat_2 = 0: Exit Function
  69. If KeyAscii = Asc(".") Then TextFormat_2 = KeyAscii: Exit Function
  70. If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  71. TextFormat_2 = 0
  72. Exit Function
  73. End If
  74. TextFormat_2 = KeyAscii
  75. End Function
  76. '=====================================================
  77. '数字_正负小数
  78. '=====================================================
  79. Private Function TextFormat_3(txtObj As TextBox,KeyAscii As Integer) As Integer
  80. If KeyAscii = 8 Then TextFormat_3 = KeyAscii: Exit Function
  81. If KeyAscii = Asc("-") And Len(txtObj.Text) > 0 Then TextFormat_3 = 0: Exit Function
  82. If KeyAscii = Asc("-") And InStr(1,"-") = 0 Then TextFormat_3 = KeyAscii: Exit Function
  83. If KeyAscii = Asc(".") And InStr(1,".") > 0 Then TextFormat_3 = 0: Exit Function
  84. If (KeyAscii = Asc(".") Or KeyAscii = Asc("0")) And Len(txtObj.Text) < 2 Then txtObj.Text = txtObj.Text & "0.": txtObj.SelStart = Len(txtObj.Text): TextFormat_3 = 0: Exit Function
  85. If KeyAscii = Asc(".") Then TextFormat_3 = KeyAscii: Exit Function
  86. If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
  87. TextFormat_3 = 0
  88. Exit Function
  89. End If
  90. TextFormat_3 = KeyAscii
  91. End Function
  92. '=====================================================
  93. '字母_任意书写
  94. '=====================================================
  95. Private Function TextFormat_4(txtObj As TextBox,KeyAscii As Integer) As Integer
  96. If KeyAscii = 8 Then TextFormat_4 = KeyAscii: Exit Function
  97. If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then
  98. TextFormat_4 = 0
  99. Exit Function
  100. End If
  101. TextFormat_4 = KeyAscii
  102. End Function
  103. '=====================================================
  104. '字母_锁定小写
  105. '=====================================================
  106. Private Function TextFormat_5(txtObj As TextBox,KeyAscii As Integer) As Integer
  107. If KeyAscii = 8 Then TextFormat_5 = KeyAscii: Exit Function
  108. If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then
  109. TextFormat_5 = 0
  110. Exit Function
  111. End If
  112. If KeyAscii >= Asc("A") And KeyAscii <= Asc("Z") Then KeyAscii = KeyAscii + 32
  113. TextFormat_5 = KeyAscii
  114. End Function
  115. '=====================================================
  116. '字母_锁定大写
  117. '=====================================================
  118. Private Function TextFormat_6(txtObj As TextBox,KeyAscii As Integer) As Integer
  119. If KeyAscii = 8 Then TextFormat_6 = KeyAscii: Exit Function
  120. If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then
  121. TextFormat_6 = 0
  122. Exit Function
  123. End If
  124. If KeyAscii >= Asc("a") And KeyAscii <= Asc("z") Then KeyAscii = KeyAscii - 32
  125. TextFormat_6 = KeyAscii
  126. End Function
  127. '=====================================================
  128. '汉字_锁定中文
  129. '=====================================================
  130. Private Function TextFormat_7(txtObj As TextBox,KeyAscii As Integer) As Integer
  131. If KeyAscii = 8 Then TextFormat_7 = KeyAscii: Exit Function
  132. If KeyAscii >= 0 And KeyAscii <= 255 Then
  133. TextFormat_7 = 0
  134. Exit Function
  135. End If
  136. TextFormat_7 = KeyAscii
  137. End Function

猜你在找的VB相关文章