C# Winform限制文本框输入数字+字母+长度
private void txtFTrayCode_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar >= '0' && e.KeyChar <= '8'|| Char.IsLetter(e.KeyChar))//输入1-8位数字或字母 { if (txtFTrayCode.Text.Length <= 7) { e.Handled = false; } else if (txtFTrayCode.SelectionLength > 1) { e.Handled = false;//选择多个 } else { e.Handled = true; } } else if (e.KeyChar == '.' || e.KeyChar == (char)Keys.Return) //Tab或回车键 { e.Handled = true; SendKeys.Send("{TAB}"); } else if (e.KeyChar == '\b' || (int)e.KeyChar == 7) e.Handled = false;//删除 else e.Handled = true; }