在 Visual Basic.NET 中禁用字母和特殊字符 [英] Disabling alphabetic letters and special characters in Visual Basic.NET

查看:36
本文介绍了在 Visual Basic.NET 中禁用字母和特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是visual basic的新手,我不确定如何禁用字母和特殊字符.

Im new to visual basic and im unsure on how to disable alphabetic and special characters.

我只希望用户能够输入数字.

I Only want the user to be able to input numbers.

我正在使用此代码,我知道有一种更简单的方法可以做到,感谢所有帮助

Im using this code im know there is an easier way to do it and all help is appreciated

我在输入字母字符和输入数字时收到消息框.我不想在输入数字时收到消息框.

I get the message box when I input alphabetic characters and when I input numbers. i dont want to get the message box when i input numbers.

Private Sub txtCustom_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCustom.KeyDown

    If (e.KeyCode = Keys.Enter) Then

        e.SuppressKeyPress = True

        If (e.KeyCode = Keys.A Or Keys.B Or Keys.C Or Keys.D Or Keys.E Or Keys.F Or Keys.G Or Keys.H Or Keys.I Or Keys.J Or Keys.K Or Keys.L Or Keys.M Or Keys.N Or Keys.O Or Keys.P Or Keys.Q Or Keys.R Or Keys.S Or Keys.T Or Keys.U Or Keys.V Or Keys.W Or Keys.X Or Keys.Y Or Keys.Z) Then

            Beep()
            MsgBox("Please Input A Numerical Value")
            txtCustom.Text = ""

        Else

            RandNumAllow = txtCustom.Text

        End If

    End If

End Sub

推荐答案

尝试改用 KeyPress 事件:

Try using the KeyPress event instead:

Private Sub TextBox1_KeyPress(sender As Object, _
                              e As KeyPressEventArgs) Handles TextBox1.KeyPress
  e.Handled = Not Char.IsNumber(e.KeyChar)
End Sub

但这不会阻止某人使用剪贴板.改用 MaskedTextBox 控件可能更好.

But this won't stop someone from using the clipboard. It's probably better to use the MaskedTextBox control instead.

这篇关于在 Visual Basic.NET 中禁用字母和特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆