VB文本框可以利用Ascii字符来限定输入的内容类型,这里我分了两大部分来阐述怎样限制数据类型,其实原理都是一样的,都是Ascii字符范围设定的问题,大家可以根据Ascii表来限制数据类型。
一、私有过程,不可调用
1、限定只输入汉字
代码:
Private Sub txtDirector_KeyPress(KeyAscii As Integer) If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then Else KeyAscii = 0 MsgBox "请输入汉字!",vbOKOnly + vbExclamation,"警告" txtDirector.SetFocus End If End Sub
这条语句用来判断输入的字符是否是汉字,如果不是汉字,就把这个输入的字符屏蔽掉。
keyAscii=0的字符是“空格”,keyAscii=8的字符是“退格”
2、限定只输入数字
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
这条语句用来判断输入的字符是否在0-9的范围,如果不在这个范围,就把这个输入的字符屏蔽掉。(。其他的地方和上面的代码原理都是一样的 。
如果输入的数字可能是小数,那么还要添加如下代码:
If KeyAscii = 46 And Not CBool(InStr(txbNumber,".")) Then Exit Sub
当输入小数点时,程序判断文本框中是否已有小数点(因为一个小数中不可能有多个小数点),如果没有小数点,则允许输入。
二、公有过程,在模块中可调用
代码:
Option Explicit Public Enum FormatType [数字_正整数] = 0 [数字_正负整数] = 1 [数字_正小数] = 2 [数字_正负小数] = 3 [字母_任意书写] = 4 [字母_锁定小写] = 5 [字母_锁定大写] = 6 [汉字_锁定中文] = 7 End Enum Function TextFormat(txtObj As TextBox,KeyAscii As Integer,Optional FormatType As FormatType = 0) As Integer Dim ReturnKeyCode As Integer Select Case FormatType Case 0 ReturnKeyCode = TextFormat_0(txtObj,KeyAscii) Case 1 ReturnKeyCode = TextFormat_1(txtObj,KeyAscii) Case 2 ReturnKeyCode = TextFormat_2(txtObj,KeyAscii) Case 3 ReturnKeyCode = TextFormat_3(txtObj,KeyAscii) Case 4 ReturnKeyCode = TextFormat_4(txtObj,KeyAscii) Case 5 ReturnKeyCode = TextFormat_5(txtObj,KeyAscii) Case 6 ReturnKeyCode = TextFormat_6(txtObj,KeyAscii) Case 7 ReturnKeyCode = TextFormat_7(txtObj,KeyAscii) End Select TextFormat = ReturnKeyCode End Function '===================================================== '数字_正整数 '===================================================== Private Function TextFormat_0(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_0 = KeyAscii: Exit Function If KeyAscii = Asc("0") And Len(txtObj.Text) = 0 Then TextFormat_0 = 0: Exit Function If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then TextFormat_0 = 0 Exit Function End If TextFormat_0 = KeyAscii End Function '===================================================== '数字_正负整数 '===================================================== Private Function TextFormat_1(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_1 = KeyAscii: Exit Function If KeyAscii = Asc("0") And Len(txtObj.Text) = 0 Then TextFormat_1 = 0: Exit Function If KeyAscii = Asc("-") And Len(txtObj.Text) > 0 Then TextFormat_1 = 0: Exit Function If KeyAscii = Asc("-") And Len(txtObj.Text) = 0 Then TextFormat_1 = KeyAscii: Exit Function If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then TextFormat_1 = 0 Exit Function End If TextFormat_1 = KeyAscii End Function '===================================================== '数字_正小数 '===================================================== Private Function TextFormat_2(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_2 = KeyAscii: Exit Function If KeyAscii = Asc(".") And InStr(1,txtObj.Text,".") > 0 Then TextFormat_2 = 0: Exit Function 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 If KeyAscii = Asc(".") Then TextFormat_2 = KeyAscii: Exit Function If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then TextFormat_2 = 0 Exit Function End If TextFormat_2 = KeyAscii End Function '===================================================== '数字_正负小数 '===================================================== Private Function TextFormat_3(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_3 = KeyAscii: Exit Function If KeyAscii = Asc("-") And Len(txtObj.Text) > 0 Then TextFormat_3 = 0: Exit Function If KeyAscii = Asc("-") And InStr(1,"-") = 0 Then TextFormat_3 = KeyAscii: Exit Function If KeyAscii = Asc(".") And InStr(1,".") > 0 Then TextFormat_3 = 0: Exit Function 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 If KeyAscii = Asc(".") Then TextFormat_3 = KeyAscii: Exit Function If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then TextFormat_3 = 0 Exit Function End If TextFormat_3 = KeyAscii End Function '===================================================== '字母_任意书写 '===================================================== Private Function TextFormat_4(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_4 = KeyAscii: Exit Function If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then TextFormat_4 = 0 Exit Function End If TextFormat_4 = KeyAscii End Function '===================================================== '字母_锁定小写 '===================================================== Private Function TextFormat_5(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_5 = KeyAscii: Exit Function If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then TextFormat_5 = 0 Exit Function End If If KeyAscii >= Asc("A") And KeyAscii <= Asc("Z") Then KeyAscii = KeyAscii + 32 TextFormat_5 = KeyAscii End Function '===================================================== '字母_锁定大写 '===================================================== Private Function TextFormat_6(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_6 = KeyAscii: Exit Function If Not ((KeyAscii >= Asc("a") And KeyAscii <= Asc("z")) Or (KeyAscii >= Asc("A") And KeyAscii <= Asc("Z"))) Then TextFormat_6 = 0 Exit Function End If If KeyAscii >= Asc("a") And KeyAscii <= Asc("z") Then KeyAscii = KeyAscii - 32 TextFormat_6 = KeyAscii End Function '===================================================== '汉字_锁定中文 '===================================================== Private Function TextFormat_7(txtObj As TextBox,KeyAscii As Integer) As Integer If KeyAscii = 8 Then TextFormat_7 = KeyAscii: Exit Function If KeyAscii >= 0 And KeyAscii <= 255 Then TextFormat_7 = 0 Exit Function End If TextFormat_7 = KeyAscii End Function原文链接:https://www.f2er.com/vb/256968.html