在C#中怎样让一个文本框控件只能输入数字,而不是其他非数字数据? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 没用过就去学下http://www.microsoft.com/china/msdn/library/webservices/asp.net/regexnet.mspx?pf=true 在textBox的KeyPress事件中写private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ if (e.KeyChar <= 57 && e.KeyChar >= 48 || e.KeyChar == 46 || e.KeyChar == 8) { e.Handled = false; } else { e.Handled = true; }}作用为只可以在textBox中输入数字,小数点,和Tab键。 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { string strText = (sender as TextBox).Text; //只能输入数字、退格和小数点 if (!char.IsDigit(e.KeyChar) && e.KeyChar!= 8 && e.KeyChar!='.') e.Handled = true; //最多只能有一个小数点 if ((e.KeyChar == '.') && (strText.IndexOf('.') > -1)) e.Handled = true; } 用过mySQL的请进! 图片问题 类的属性和类的实例属性的区别!!! 如何在点击一个按钮后调出数据库中的第一条记录和最后条记录!!!!!!!!!!1111 开发Word插件用什么方法最简便 SOS 如何用C#代码在Excel中插入一列waiting…… mdi窗体问题 C# 错误提示 lunece.net同义词搜索功能 如何控制焦点,急! 请教高人Bitmap怎么利用socket传出去? ****** 我现在遇到一个问题。在 winform 下,想让一个文本框中只能输入数字,可以带小数点。
http://www.microsoft.com/china/msdn/library/webservices/asp.net/regexnet.mspx?pf=true
{
if (e.KeyChar <= 57 && e.KeyChar >= 48 || e.KeyChar == 46 || e.KeyChar == 8)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
作用为只可以在textBox中输入数字,小数点,和Tab键。
{
string strText = (sender as TextBox).Text;
//只能输入数字、退格和小数点
if (!char.IsDigit(e.KeyChar) && e.KeyChar!= 8 && e.KeyChar!='.')
e.Handled = true;
//最多只能有一个小数点
if ((e.KeyChar == '.') && (strText.IndexOf('.') > -1))
e.Handled = true;
}