我只想制作能够在 vb6 中的文本框中使用的预定义字符。我怎样才能做到这一点?
预定义的字符将是 like0-9
和A, C, M, E
除这些之外的所有其他字符将给出一个 msgbox 作为 err。它也可以是a,c,m,e
我可以Ucase()
用来解决它。
你可以;
private Sub atextbox_KeyPress(keyascii As Integer)
if InStr(1, "0123456789ACME", Chr$(keyascii)) = 0 Then keyascii = 0 '//case sensitive
End Sub
或者
if Chr$(keyascii) like "[0-9]" or Chr$(keyascii) like "[ACMEacme]"
交替格式化
select case true
case chr$(keyascii) like "[0-9]"
case chr$(keyascii) like "[ACMEacme]"
case else
keyascii = 0
end select
您可以检测使用KeyPress
事件输入的每个字符并检查 ASCII 值。如果将其设置为 0,则按下将被忽略。请务必检查Change
事件以捕捉粘贴等。
另外,不要使用消息框,因为这会惹恼用户。
使用KeyPress事件:
Private Sub txtBox_KeyPress(KeyAscii As Integer)
Dim KeyChar As String
If KeyAscii > 31 Then 'ignore low-ASCII characters like BACKSPACE
KeyChar = Chr(KeyAscii)
If Not IsAllowed(KeyChar) Then
KeyAscii = 0
MsgBox.Show("The allowed characters are ... ")
End If
End If
End Sub
IsAllowed 函数将包含允许的键代码。