在 visual basic 2012 OOP 方法上按键阻止 letter/number

keypress to block letter/number on visual basic 2012 OOP method

我想使用 visual studio 2012 和 SQL 服务器 2012 创建注册表单。

我想要的功能是: 在 windows 表单中有两个文本框:

听说要用按键。

按键的编码是什么?

这应该可以解决问题:

Public Class Form1

    Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If IsNumeric(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

    Private Sub TextBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox2.KeyPress
        If Not IsNumeric(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub

End Class

您需要使用 ASCII 值来执行此操作:

If Asc(e.KeyChar) <> 8 Then
    If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
        e.Handled = True
    End If
End If

在上面的例子中,使用只能输入数字,可以使用BackSpace,因为这个函数将只允许在KeyPress Event上使用这些ASCII字符。

8 -> 退格键, 48-57 -> 0 到 9

对于从 a 到 z(大写和小写)的字符同样使用 ASCII 字符