Tuesday, January 27, 2009

Limiting WPF Textbox Input character

Today, I spend almost 2 hours looking for answers.
I look like stupid on how to control or validate the accepted characters that the textbox
would accept. A very simple problem in the good old days.

i got some answers from

http://www.vbdotnetforums.com/windows-presentation-foundation-wpf/23881-ascii-value-instead-system-windows-input-key.html

I appreciate the solution above, but some has to validate what kind of characters are
accepted.

but hey, it's much as easy as it would be, the PreviewTextInput() event is the right answer;
giving the correct character pressed on the keyboard and is not returning the
Windows.Input.Key types...

Here is the sample.


Private Sub Textbox_PreviewTextInput(ByVal sender As Object, ByVal e As System.Windows.Input.TextCompositionEventArgs) Handles Me.PreviewTextInput
If e.Text = "a" ' will not accept "a"
e.Handled = True
Else
e.Handled = False
End If
End Sub


Sorry for the HTML format..
Enjoy.