Question Keystroke Validation

leenej

New member
Joined
Nov 17, 2011
Messages
2
Programming Experience
Beginner
Hi everyone,
How do I block certain keys in a textbox? Sorry, I'm new to WPF and Visual Studio 2010.

I used the PreviewKeyDown event, since I can't find a KeyPress event to capture keystokes on the textbox.
VB.NET:
Private Sub TextBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles TextBox1.PreviewKeyDown
 
End Sub

What I want to do is a function to filter the keystoke, say, block all letters, or numbers and punctuation marks,etc..
What I'd usually do is this in Windows Form Applications:
VB.NET:
    Public Sub ValidateKey(ByVal IsType As Short, ByRef myKey As KeyPressEventArgs)
        Dim keySeries As String
        Select Case IsType
            Case 1
                keySeries = "abcdefghijklmnopqrstuvwxyzñ. "
                If myKey.KeyChar <> ControlChars.Back AndAlso keySeries.IndexOf(LCase(myKey.KeyChar), 0) < 0 Then
                    myKey.Handled = True
                End If
            Case 2
                keySeries = "1234567890"
                If myKey.KeyChar <> ControlChars.Back AndAlso keySeries.IndexOf(LCase(myKey.KeyChar), 0) < 0 Then
                    myKey.Handled = True
                End If
            Case 3
                keySeries = "1234567890."
                If myKey.KeyChar <> ControlChars.Back AndAlso keySeries.IndexOf(LCase(myKey.KeyChar), 0) < 0 Then
                    myKey.Handled = True
                End If
        End Select
    End Sub

But since e.Key only returns a numeric value and not a Character, I cant compare it to a set of characters.
I hope I explained it.
I appreciate any help. Thank you.
 
Back
Top