How to avoid taking special characters in textbox of a Windows application

13,311

Delete the sub which is handling KeyDown event and replace the sub which is handling KeyPress event to this one:

ReadOnly ValidChars As String = _
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,"

Private Sub txtOLDBuildingName_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) _
Handles txtOLDBuildingName.KeyPress

    e.Handled = Not (ValidChars.IndexOf(e.KeyChar) > -1 _
                OrElse e.KeyChar = Convert.ToChar(Keys.Back))

End Sub

Update:

This modification is more precise, it compares the clipbard content before paste them.

ReadOnly AllowedKeys As String = _
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,"

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

    Select Case e.KeyChar

        Case Convert.ToChar(Keys.Enter) ' Enter is pressed
            ' Call method here...

        Case Convert.ToChar(Keys.Back) ' Backspace is pressed
            e.Handled = False ' Delete the character

        Case Convert.ToChar(Keys.Capital Or Keys.RButton) ' CTRL+V is pressed
            ' Paste clipboard content only if contains allowed keys
            e.Handled = Not Clipboard.GetText().All(Function(c) AllowedKeys.Contains(c))

        Case Else ' Other key is pressed
            e.Handled = Not AllowedKeys.Contains(e.KeyChar)

    End Select

End Sub
Share:
13,311
Nilesh B
Author by

Nilesh B

Updated on June 05, 2022

Comments

  • Nilesh B
    Nilesh B almost 2 years

    I am developing the Windows application. I have a form and I am trying to validate the text box on that form.

    I want to put some validation on a text box like the text box should accept only Alphabates, Didgits and comma.(No other characters like special symbols.) As well, it should accept the Enter key when cursor is in that text box.

    I am trying to write the code but some how its not working. But its still taking special characters like <>/;' What changes I have to made ?

    here is the code...

    Key Down Event

     Private Sub txtOLDBuildingName_KeyDown(sender As Object, e As KeyEventArgs) Handles txtOLDBuildingName.KeyDown
    
            ' Initialize the flag to false.
            nonNumberEntered = False
    
            ' Determine whether the keystroke is a number from the top of the keyboard. 
            If (e.KeyCode < Keys.D0 And e.KeyCode > Keys.D9) And (e.KeyCode > Keys.A And e.KeyCode < Keys.Z) Then
                nonNumberEntered = True
            End If
            'If shift key was pressed, it's not a number. 
            If Control.ModifierKeys = Keys.Shift Then
                nonNumberEntered = True
            End If
    
    
        End Sub
    

    Key Press Event

      Private Sub txtOLDBuildingName_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtOLDBuildingName.KeyPress
               If nonNumberEntered = True Then
                   e.Handled = True
            End If
    
       End Sub