You can also add a check to be able to control characters:
if (Char.IsControl(e.KeyChar) != true && Char.IsNumber(e.KeyChar) != true)
{
e.Handled = true;
}
Update: In response to man-b's comment on s / he code, he offers the following style (which I would also personally write):
if (!Char.IsControl(e.KeyChar) && !Char.IsNumber(e.KeyChar))
{
e.Handled = true;
}
source
share