110,534
社区成员
发帖
与我相关
我的任务
分享
if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8)
e.Handled = true;
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
//检测是否已经输入了小数点
bool IsContainsDot = this.textBox2 .Text.Contains(".");
if ((e.KeyChar < 48 || e.KeyChar > 57) && (e.KeyChar != 8) && (e.KeyChar != 46))
{
e.Handled = true;
}
else if (IsContainsDot && (e.KeyChar == 46)) //如果输入了小数点,并且再次输入
{
e.Handled = true;
}
}