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 2

    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
      • Maybe a mask? sql.ru/forum/actualthread.aspx?tid=693983 - embarcadero
      • Is there any other way without a mask is allowed using regular expressions? - Demon