How to allow the input in the text field only real numbers using regional parameters. Ie, so that you can enter both a period and a comma.
2 answers
Use Int32.Parse . Procurement:
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { try { number = Int32.Parse(e.Text, culture.NumberFormat); e.Handled = true; } catch(FormatException) { } }
|
Alternatively, you can disable all characters except numbers. An example of clipping Latin characters. Inside a button press event:
if ((e.KeyChar >= 'A') && (e.KeyChar <= 'z')) { e.Handled = true; // Π½Π΅ Π²Π²ΠΎΠ΄ΠΈΠΌ Π½Π°ΠΆΠ°ΡΡΠΉ ΡΠΈΠΌΠ²ΠΎΠ» Π² ΠΏΠΎΠ»Π΅ MessageBox.Show("ΠΠ²Π΅Π΄ΡΠ½ Π»Π°ΡΠΈΠ½ΡΠΊΠΈΠΉ ΡΠΈΠΌΠ²ΠΎΠ»", "ΠΡΠΈΠ±ΠΊΠ°", MessageBoxButtons.OK, MessageBoxIcon.Error); //Π²ΡΠ²ΠΎΠ΄ΠΈΠΌ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ }
- e.KeyChar in WPF is not. - Demon
- Both in the first and in the second case I will receive an integer on the output. And how to get real without entering letters and other characters with the ability to enter a period, comma and minus? - Demon
- Is there any other way without a mask is allowed using regular expressions? - Demon
|