Reputation: 1033
I have a textbox and I need the user to enter only Cyrillic letters. User can't enter numbers and special characters (except space) and latin characters! The Value of variable "l" I will set by myself.
How can I make the KeyDown event to do this?
In WindowsForms I do it like this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char l = e.KeyChar;
if ((l < 'А' || l > 'я') && l != '\b' )
{
e.Handled = true;
}
}
Upvotes: 1
Views: 206
Reputation: 4567
The most simple way I discovered is to leverage the OnPreviewTextInput
event:
Markup:
<TextBox PreviewTextInput="UIElement_OnPreviewTextInput" />
Handler:
private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
bool isCyrillic = Regex.IsMatch(e.Text, @"\p{IsCyrillic}");
e.Handled = !isCyrillic;
}
Upvotes: 1