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;
}
}
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.