vb6中只允许字符串中的某些字符

发布于 2024-12-21 06:31:29 字数 199 浏览 2 评论 0原文

我只想制作预定义的字符,以便能够在 vb6 中的文本框中使用。 我怎样才能做到这一点?

预定义的字符将类似于 0-9A、C、M、E 除了这些字符之外的所有其他字符都会给出一个 msgbox 作为错误。它也可以是a,c,m,e我可以使用Ucase()来解决它。

I wanna make only predefined characters to be able to used on my textbox in vb6.
How can i achive that?

Predefined characters will be like 0-9 and A, C, M, E all other characters besides these gonna give a msgbox as err. it can also be a,c,m,e i can use Ucase() to solve it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤芳又自赏 2024-12-28 06:31:29

你可以;

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

You can;

private Sub atextbox_KeyPress(keyascii As Integer)
   if InStr(1, "0123456789ACME", Chr$(keyascii)) = 0 Then keyascii  = 0 '//case sensitive
End Sub

or

if Chr$(keyascii) like "[0-9]" or Chr$(keyascii) like "[ACMEacme]"

alternatively formatted

select case true
    case chr$(keyascii) like "[0-9]"
    case chr$(keyascii) like "[ACMEacme]"
    case else
        keyascii = 0
end select
幸福丶如此 2024-12-28 06:31:29

您可以使用 KeyPress 事件检测输入的每个字符并检查 ASCII 值。如果将其设置为 0,则按下操作将被忽略。
请务必检查 Change 事件以捕获粘贴等。

此外,不要使用消息框,因为这会惹恼用户。

You can detect each character entered using the KeyPress event and checking the ASCII value. If you set it to 0, the press will be ignored.
Be sure to also check in the Change event to catch pasting, etc.

Also, don't use a messagebox as this will annoy users.

挽袖吟 2024-12-28 06:31:29

使用 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 函数将包含允许的键代码。

Use the KeyPress event:

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

The IsAllowed function will contain the allowed key codes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文