Search code examples
vb.nettextbox

Removing Number from Textbox


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

Solution

  • 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