vb.net 在 Visual Basic.NET 中禁用字母和特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12997290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:00:09  来源:igfitidea点击:

Disabling alphabetic letters and special characters in Visual Basic.NET

vb.net

提问by Zukrooo

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

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

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

回答by LarsTech

Try using the KeyPress event instead:

尝试改用 KeyPress 事件:

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

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

但这不会阻止某人使用剪贴板。最好改用 MaskedTextBox 控件。

回答by The Only Smart Boy

Sorry for the late response this code will solve all the issues, copying from clipboard and shift + number for special characters as it uses the TextChanged event try it out.

抱歉,此代码将解决所有问题,因为它使用 TextChanged 事件,所以从剪贴板复制和特殊字符的 shift + number 复制,此代码将解决所有问题。

    Dim charactersAllowed As String = "1234567890"
      Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Dim theText As String = TextBox1.Text
    Dim Letter As String
    Dim SelectionIndex As Integer = TextBox1.SelectionStart
    Dim Change As Integer
    For x As Integer = 0 To TextBox1.Text.Length - 1
        Letter = TextBox1.Text.Substring(x, 1)
        If charactersAllowed.Contains(Letter) = False Then
            SystemSounds.Beep.Play()
            theText = theText.Replace(Letter, String.Empty)
            Change = 1
        End If
    Next
    TextBox1.Text = theText
    TextBox1.Select(SelectionIndex - Change, 0)
End Sub

You can replace the value of charractersAllowed with whatever characters you want to allow

您可以将 charactersAllowed 的值替换为您想要允许的任何字符

回答by chervani

The code you were looking for I think looks like this.

您正在寻找的代码我认为看起来像这样。

For the keypress event on whatever text or input box your trying to control input into

对于您试图控制输入的任何文本或输入框上的按键事件

If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso 'restricts anything but number keys
    e.KeyChar <> ControlChars.Back Then 'allows use of backspace
    e.Handled = True
End If