user38349
user38349

Reputation: 3025

Trap Key Entries to Digits and Cap Alphas in Textbox

In WPF need to trap the keys entered in a textblock to keep the user from entering anything but digits,cap letters and navigation keys (Backspace, Arrows, etc).

Thanks!

Upvotes: 1

Views: 469

Answers (1)

RS Conley
RS Conley

Reputation: 7194

First there is a Filtered Text Control available here that does simple masking

The simple way is to handle the PreviewTextInput Event with something like this.

Private Sub TextBox1_PreviewTextInput(ByVal sender As Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles TextBox1.PreviewTextInput
    Dim Character As Char = Convert.ToChar(e.Text)
    If Char.IsDigit(Character) Then
        e.Handled = False
    ElseIf Char.IsLetter(Character) And UCase(Character) = Character Then
        e.Handled = False
    Else
        e.Handled = True
    End If


End Sub

Upvotes: 3

Related Questions