I have tried this code to restrict only numbers.It type only numbers and don’t make entry when we try to enter characters or any other controls, even it doesnt enter backspace also. how to prevent backspace from it.
private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}
You do not need to use a RegEx in order to test for digits:
To allow for backspace:
If you want to add other allowable keys, look at the
Keysenumeration and use the approach above.