vb文本框如何禁止输入特定字符

来源:百度知道 编辑:UC知道 时间:2024/06/25 11:22:25

VB文本框可通过文本框的KeyPress事件来判断输入字符的KeyAscii值来禁止输入特定字符。

代码实例,禁止输入大写A字符:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 65 Then
        KeyAscii = 0
    End If
End Sub
  • KeyPress 事件,此事件当用户按下和松开一个 ANSI 键时发生。

  • KeyAscii    
    整数,包含引发 AccessKeyPress 事件的按键(除 ALT 键外)的 Ascii 值,与标准的 KeyPress事件采用的方法相同。    

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub 可以禁止输入字母

private sub text1_change()
if text1=”" then exit sub
if not isletter(text1) then
msgbox “only a-z A-Z letters allowed”
end if
end sub 这个是禁止输入算字和特殊符号

应该是可以的,只要你把自己想禁止的字母,符号等的ASCLL码的值知道了,,就可以了…

if text1.text