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