原创 VB.Net下用正则表达式阻止非法字符输入

2008-11-14 19:47 4014 8 8 分类: 软件与OS

点击开大图


    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim ch As Char = e.KeyChar
        If Not Regex.IsMatch(ch.ToString(), "^[\b\r0-9a-fA-F]$") Then
            e.KeyChar = "" '放弃输入的非法字符
            MessageBox.Show("请正确输入16进制数!!!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            e.KeyChar = Char.ToUpper(ch) '强制转换为大写字母,放过退格和回车符
        End If
    End Sub

文章评论0条评论)

登录后参与讨论
我要评论
0
8
关闭 站长推荐上一条 /2 下一条