textBox.KeyPress += (sender, e) => e.Handled = !char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar);
This will only permit the use of digits and control characters in the TextBox
, other combinations are possible using the same approach of setting the Handle
property to true to block the text.
The user can still copy/paste unwanted characters so an additional check should be on the TextChanged
to cleanse the input:
textBox.TextChanged += (sender, e) => textBox.Text = Regex.Match(textBox.Text, @"\d+").Value
In this example a Regular expression is used to filter the text.
NumericUpDown should be preferred for numbers when possible.