How to prevent the insertion into the textBox of everything except numbers? That is, so that I, for example, copied "Hello world" and could not paste it into the textBox from their clipboard, and if I copy 423, 23, 2, etc., I could. I only know how to allow input of only numbers from the keyboard, but I don’t understand from the buffer.

    1 answer 1

    To enter numeric values ​​in WinForms there is a special control: NumericUpDown .
    When using it, you no longer need all sorts of input validations.

    An alternative method using ordinary TextBox involves handling the TextBox.TextChanged event. In the handler, we check whether valid text is contained in the TextBox.Text property and, depending on this, we allow or prohibit further work. For example, change the Enabled property of the "OK" button.

    • Using a regular expression? - Andrei
    • @Andrey With the help of anything. Personally, I would prefer NumericUpDown , but if it is so important to use TextBox , then int.TryParse for whole or double.TryParse for doubles. - Dmitry D.