Numeric text block in C # - WPF

I want to create a text field in WPF that will only accept numbers ... I'm reascerhed and people say to use a keypress event or a masked text field, but they are in window forms ...

+6
source share
1 answer

For WPF:

private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e) { if (!char.IsDigit(e.Text, e.Text.Length - 1)) e.Handled = true; } 

For Windows Forms:

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsDigit(e.KeyChar) ) e.Handled = true; } 
+33
source

Source: https://habr.com/ru/post/948852/


All Articles