VB文本框限制输入类型

VB文本框限制输入类型,第1张

概述VB文本框可以利用Ascii字符来限定输入的内容类型,这里我分了两大部分来阐述怎样限制数据类型,其实原理都是一样的,都是Ascii字符范围设定的问题,大家可以根据Ascii表来限制数据类型。 一、私有过程,不可调用 1、限定只输入汉字 代码: Private Sub txtDirector_KeyPress(KeyAscii As Integer)If KeyAscii >= -20319 An

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 IfEnd Sub



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

2、限定只输入数字


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

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



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


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

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

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

代码:

Option ExplicitPublic Enum FormatType    [数字_正整数] = 0    [数字_正负整数] = 1    [数字_正小数] = 2    [数字_正负小数] = 3    [字母_任意书写] = 4    [字母_锁定小写] = 5    [字母_锁定大写] = 6    [汉字_锁定中文] = 7End EnumFunction textformat(txtObj As TextBox,KeyAscii As Integer,Optional FormatType As FormatType = 0) As IntegerDim 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 = ReturnKeyCodeEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd 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 = KeyAsciiEnd Function
总结

以上是内存溢出为你收集整理的VB文本框限制输入类型全部内容,希望文章能够帮你解决VB文本框限制输入类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1266457.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存