Get a textbox to accept only characters in vb.net

℡╲_俬逩灬. 提交于 2019-12-08 06:35:50

问题


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.

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?


回答1:


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 ASCII way, try this,

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.




回答2:


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!




回答3:


This is a more abstract approach, but still effective nonetheless. It's straightforward and can just be added to the TextChanged event. You can use it with multiple textboxes by adding their handles to the sub and using a 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).




回答4:


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


来源:https://stackoverflow.com/questions/12633780/get-a-textbox-to-accept-only-characters-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!