首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB编写的怎么进行数据过滤,判断输入是字母

2012-06-06 
VB编写的如何进行数据过滤,判断输入是字母遇到一个问题,要求keypress事件,对输入的数据进行过滤。如果输入

VB编写的如何进行数据过滤,判断输入是字母
遇到一个问题,要求keypress事件,对输入的数据进行过滤。如果输入的是字母,则显示输入正确;如果输入的是非字母,则响铃且消除该字符。
这个怎么判断字母呢?按照asc函数?那如果输入的是数字呢?

[解决办法]

VB code
Private Sub Text1_KeyPress(KeyAscii As Integer)    If Not Chr(KeyAscii) Like "[a-zA-Z]" Then       KeyAscii = 0       Debug.Print "输入错误!"    Else       Debug.Print "输入正确!"    End IfEnd Sub
[解决办法]
这样也行:
VB code
Private Sub Text1_KeyPress(KeyAscii As Integer)    If (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then        Debug.Print "输入正确!"    Else        KeyAscii = 0        Debug.Print "输入错误!"    End IfEnd Sub
[解决办法]
探讨
这样也行:

VB code
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then
Debug.Print "输入正确!"
Else
……

[解决办法]
据说在 VB 中,IF 语句中的逻辑表达式是会全部运算完的。

我觉得这样写运行效率会高些吧:
VB code
Private Sub Text1_KeyPress(KeyAscii As Integer)   Dim i%   i = KeyAscii Or 32   If (i >= 97 And i <= 122) Then      Debug.Print "输入正确!"   Else      KeyAscii = 0      Debug.Print "输入错误!"   End IfEnd Sub 

热点排行