获取文本框以仅接受 vb.net 中的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12633780/
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-09 16:19:03  来源:igfitidea点击:

Get a textbox to accept only characters in vb.net

vb.netasciikeypress

提问by EqEdi

I'm creating a simple application in Vb.net where I need to perform certain validations. So I want a name textbox to accept only characters from a-z and A-Z for example.

我正在 Vb.net 中创建一个简单的应用程序,我需要在其中执行某些验证。所以我想要一个名称文本框只接受来自 az 和 AZ 的字符。

For this I wrote the following code:

为此,我编写了以下代码:

   Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress
    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) > 65 Or Asc(e.KeyChar) < 90 Or Asc(e.KeyChar) > 96 Or Asc(e.KeyChar) < 122 Then
            e.Handled = True
        End If
    End If
End Sub

But somehow it is not allowing me to enter characters. When I try to enter any character it does nothing.

但不知何故,它不允许我输入字符。当我尝试输入任何字符时,它什么也不做。

What causes this problem and how can I resolve it?

是什么导致了这个问题,我该如何解决?

回答by John Woo

Alternatively, you can do something like this,

或者,你可以做这样的事情,

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) _
                              Handles txtName.KeyPress

    If Not (Asc(e.KeyChar) = 8) Then
        Dim allowedChars As String = "abcdefghijklmnopqrstuvwxyz"
        If Not allowedChars.Contains(e.KeyChar.ToString.ToLower) Then
            e.KeyChar = ChrW(0)
            e.Handled = True
        End If
    End If

End Sub

or if you still want the ASCIIway, try this,

或者如果你仍然想要这ASCII条路,试试这个,

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtName.KeyPress

    If Not (Asc(e.KeyChar) = 8) Then
        If Not ((Asc(e.KeyChar) >= 97 And Asc(e.KeyChar) <= 122) Or (Asc(e.KeyChar) >= 65 And Asc(e.KeyChar) <= 90)) Then
            e.KeyChar = ChrW(0)
            e.Handled = True
        End If
    End If

End Sub

both will behave the same.

两者的行为都是一样的。

回答by charles

Private Sub txtname_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox5.KeyPress

    If AscW(e.KeyChar) > 64 And AscW(e.KeyChar) < 91 Or AscW(e.KeyChar) > 96 And AscW(e.KeyChar) < 123 Or AscW(e.KeyChar) = 8 Then 
    Else
        e.KeyChar = Nothing
    End If

End Sub

I hope this helps!

我希望这有帮助!

回答by Dog Lover

This is a more abstract approach, but still effective nonetheless. It's straightforward and can just be added to the TextChangedevent. You can use it with multiple textboxes by adding their handles to the sub and using a DirectCast().

这是一种更抽象的方法,但仍然有效。它很简单,可以添加到TextChanged事件中。您可以通过将其句柄添加到 sub 并使用DirectCast().

Dim allowed As String = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
        For Each c As Char In TextBox.Text
            If allowed.Contains(c) = False Then
                TextBox.Text = TextBox.Text.Remove(TextBox.SelectionStart - 1, 1)
                TextBox.Select(TextBox.Text.Count, 0)
            End If
        Next

Summary:If an invalid character is entered, it will immediately be deleted (most of the time the character won't be visible long enough for the user to notice).

总结:如果输入了无效字符,它将立即被删除(大多数情况下,该字符的可见时间不足以让用户注意到)。

回答by Purushottam Kumar

Private Sub txtStudentName_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStudentName.KeyPress

  If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = Chr(Keys.Delete) And Not e.KeyChar = Chr(Keys.Back) And Not e.KeyChar = Chr(Keys.Space) Then

      e.Handled = True

  End If

End Sub