Ignoring Characters in a TexBox

The code below shows you how to ignore all input unless it’s a number:

private void searchText_KeyPress(object sender, KeyPressEventArgs e)
{
 if (e.KeyChar != 8)
 {
  if (char.IsNumber(e.KeyChar) == false)
  {
   e.Handled = true;
}
}

I allow the backspace keypress by checking for character 8. You can easily modify this code to suit your needs.

 

Tip Submitted By: David McCarter