if (!Char.IsLetterOrDigit(e.KeyChar) && !Char.IsPunctuation(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != (char)Keys.Back) //IsNumber仅数字
{
e.Handled = true; //获取或设置一个值,指示是否处理过System.Windows.Forms.Control.KeyPress事件
}
else if (Char.IsPunctuation(e.KeyChar))
{
if (e.KeyChar == '.')
{
if (((TextBox)sender).Text.LastIndexOf('.') != -1)
{
e.Handled = true;
}
}
else
{
e.Handled = true;
}
}
限制输入类型 IsNumber是仅数字 IsLetterOrDigit是英文字母和数字 IsPunctuation是标点符号 Back是键盘回格键
些列小数点可用
-----------------------------------------------------------
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= (char)Keys.D0 && e.KeyChar <= (char)Keys.D9 || e.KeyChar == (char)Keys.Back)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
限制文本框输入类型只能输入数字