private void txtPinCode_KeyPress(object sender, KeyPressEventArgs e) { // only allows numbers 0-9 if (!char.IsDigit(e.KeyChar) && (e.KeyChar != Convert.ToChar(Keys.Back))) e.Handled = true; }