textBox文本框限制只输入数字

  • 格式:txt
  • 大小:1.51 KB
  • 文档页数:1

点击所需设置的文本框,单击事件,双击KeyPress,输入下面代码




private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == ' ' || e.KeyChar == ',' || e.KeyCher ==(char)8)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}



限制输入数字和小数点
private void txtmoney_KeyPress(object sender, KeyPressEventArgs e)
{
string str = txtmoney.Text;
int a = e.KeyChar;
if (a >= 48 && a <= 57 || a == 46 || a == 13 || a == 8)
{

if (a == 46 && str.Contains("."))
e.Handled = true;
else
{
if (a == 46 && str == "")
{
txtmoney.Text = "0";
txtmoney.SelectionStart = 1;
}
else
e.Handled = false;
}
if (str.IndexOf('.')!=-1 && (str.IndexOf('.') + 3) == str.Length)
{
e.Handled = true;
}

}
else
{

e.Handled = true;
}
}













获取按键ASCII码

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char a = e.KeyCher;
int i = a;
textBox1.Text = i.Tostring();
}

下载文档原格式

  / 1
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。