简体   繁体   English

VB.NET 中的仅字母文本框

[英]Letter only textbox in VB.NET

How would you write a regular expression in Visual Basic .NET to only accept upper and lower case letters on button click?您将如何在 Visual Basic .NET 中编写仅接受按钮单击时的大写和小写字母的正则表达式?

My text box can't accept numbers, blank spaces, or special characters.我的文本框不能接受数字、空格或特殊字符。

In order to forbid a user type anything but letters into a text box field, just add the KeyPress event for your TextBox:为了禁止用户在文本框字段中输入除字母以外的任何内容,只需为您的文本框添加KeyPress事件:

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar <> vbBack And Char.IsLetter(e.KeyChar) = False Then
        e.Handled = True
    End If
End Sub

Char.IsLetter is checking if the character typed is a letter. Char.IsLetter正在检查输入的字符是否为字母。 If the character the user typed is not a letter ( = False ), we omit passing it to the field by setting e.Handled to True .如果用户输入的字符不是字母 ( = False ),我们通过将e.Handled设置为True来省略将其传递给字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM