Reputation: 2504
Trying to use the following class to limit a textbox to text only:
However, it sees that its not text and e.Handled is false but its leaving the number in the textbox. How can i remove it?
Public Class LettersOnlyTextbox
Inherits TextBox
Public Class LettersOnlyTextbox
Inherits TextBox
Protected Overrides Sub onkeydown(e As System.Windows.Forms.KeyEventArgs)
Dim c = Convert.ToChar(e.KeyValue)
Select Case e.KeyCode
Case Keys.Back, Keys.Delete
e.Handled = False
Case Else
e.Handled = Not Char.IsLetter(c)
End Select
End Sub
End Class
Upvotes: 0
Views: 158
Reputation: 84
Private Sub MyTextBox_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyTextBox.KeyPress
If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
e.Handled = True
End If
End Sub
Upvotes: 0
Reputation: 119
Here's exactly how I handle restricting text within a text box. It's actually quite simple and I use the textbox keypress handler. I'll also include a picture to show you how to get to it.
Please note "AllowedChars" is the variable that you use to determine what characters you want to "allow" the user to be able to enter into the text box. If they try to press any other key, it simply won't go into the textbox.
With this method it also allows the use of backspace and the shift key for Capital letters.
Private Sub Textbox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
Dim AllowedChars As String = "abcdefghijklmnopqrstuvwxyz" 'Change the value of this variable to suit your needs.
If e.KeyChar <> ControlChars.Back and ModifierKeys <> Keys.Shift Then
If AllowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True 'This is what prevents the keys from being entered into the textbox
Else
End If
End If
End Sub
If you do not want the backspace or the shift key being allowed then simply remove that part of the code as such:
If AllowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True 'This is what prevents the keys from being entered into the textbox
Else
End If
Also note that this does not prevent copy and paste into the form, so you still want to put in error preventative methods when you include your actions/submits/buttons (however you want to state it lol).
The pictures at this point are to direct you to the keypress event handler.
Hopefully this helps you! Cheers :)
Upvotes: 0
Reputation: 2504
I was able to solve it this way
Protected Overrides Sub onkeydown(e As System.Windows.Forms.KeyEventArgs)
Dim c = Convert.ToChar(e.KeyValue)
Select Case e.KeyCode
Case Keys.Back, Keys.Delete
e.Handled = False
Case Else
If Not Char.IsLetter(c) Then
e.SuppressKeyPress = True
End If
End Select
End Sub
Upvotes: 1
Reputation: 2701
If you set Handled = False, it sends the event to the operating system to be handled by default. So, you want the opposite to be true to STOP something from being entered in the textbox.
Therefore, you want...
e.Handled = Not Char.IsLetter(c)
'if the character is not a letter then handle it (i.e. stop)
You would also want to change the statement above it for the Back and Delete keys to be False
Upvotes: 0