c# textbox 只能 输入数字,退格,小数点 小数点后有两位,整数部分有六位 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$"); if (!reg.IsMatch(textBox1.Text.Trim())) { MessageBox.Show("请输入正确的数字"); textBox1.Focus(); return; } else { MessageBox.Show("输入正确!"); } Regex regex = new Regex(@"^\s*?\d{1,6}\.[\d]{1,2}\s*?$"); if (!regex.IsMatch(textBox1.Text)) { MessageBox.Show("Error"); textBox1.SelectAll(); return; } Console.WriteLine(textBox1.Text.Trim()); 以下代码可以禁止输入非要求的其他内容(放在KeyDown事件中),模式检测可以在TextChanged事件中,如1,2楼代码 if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Back) { } else { e.SuppressKeyPress = true; } 顶楼上,在KeyDown事件中判断,通过e.KeyValue得到按键值,判断是否是数字或者小数点,如果不是在keyUp事件中清楚达不到要求的文本 用自带NumericUpAndDown岂不是更好,更方便? 如果我输入2.0.34.56呢? 这咋办?1楼2楼的也是欠缺考虑的.写的不够严谨. 没有完全达到楼主要求.正则太浪费计算了. 建议还是直接写逻辑判断吧. string last = "";//记录最后一次输入符合要求的数字 decimal max = new decimal(999999.99);//设置最大可输入 decimal min = new decimal(0);//设置最小可输入 int dot = 2;//小数点之后保留2位 private void textBox1_TextChanged(object sender, EventArgs e) { if (this.textBox1.Text == last) return; if (this.textBox1.Text != null) { decimal d; if (decimal.TryParse(this.textBox1.Text, out d)) { if (d > max || d < min) { MessageBox.Show("数字只能在" + min + " - " + max + "之间(包括" + min + "和" + max + ")."); this.textBox1.Text = last; return; } int p = this.textBox1.Text.IndexOf('.'); if (p != -1 && p < this.textBox1.Text.Length - dot -1) { MessageBox.Show("小数最多2位"); this.textBox1.Text = last; return; } } else { MessageBox.Show("只能输入数字"); this.textBox1.Text = last; return; } } last = this.textBox1.Text; }如果懒的话,直接系统控件 NumericUpDownDecimalPlaces = 2;//小数位数为2Maximum = new decimal(999999.99);//最大数字为6位整数+2位小数的9 decimal number = 0;if (decimal.TryParse(strCharge, out number) == true){}else{}最好写在textbox的valided事件中 ◆100分◆WinForm通过什么更新1000万以上Sqlever数据表效率会高些? 关于2011齐鲁软件大赛题目,有兴趣朋友进来讨论下吧!!! DataGridView绑定记录集后,如何清空里面数据 c# DATASET保留小数 请问如何创建和编辑如下xml文档 问个问题 高手帮忙看看 c#怎么实现excel作业自动批阅 请高手帮我分析一下代码,错在哪里?(数据库删除操作) 为什么服务器控件能实现平常时现不了的功能? ■■■为什么OnPaint不工作? 用c#写的webservice,给IPHONE客户端调用,string参数为空,值类型参数正常 C# 怎么刷新主窗体
Regex reg = new Regex(@"^[0-9]+(.[0-9]{2})?$");
if (!reg.IsMatch(textBox1.Text.Trim()))
{
MessageBox.Show("请输入正确的数字");
textBox1.Focus();
return;
}
else
{
MessageBox.Show("输入正确!");
}
Regex regex = new Regex(@"^\s*?\d{1,6}\.[\d]{1,2}\s*?$");
if (!regex.IsMatch(textBox1.Text))
{
MessageBox.Show("Error");
textBox1.SelectAll();
return;
}
Console.WriteLine(textBox1.Text.Trim());
if ((e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)
|| (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
|| e.KeyCode == Keys.OemPeriod
|| e.KeyCode == Keys.Back)
{
}
else
{
e.SuppressKeyPress = true;
}
decimal max = new decimal(999999.99);//设置最大可输入
decimal min = new decimal(0);//设置最小可输入
int dot = 2;//小数点之后保留2位 private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text == last)
return; if (this.textBox1.Text != null)
{
decimal d;
if (decimal.TryParse(this.textBox1.Text, out d))
{
if (d > max || d < min)
{
MessageBox.Show("数字只能在" + min + " - " + max + "之间(包括" + min + "和" + max + ").");
this.textBox1.Text = last;
return;
}
int p = this.textBox1.Text.IndexOf('.');
if (p != -1 && p < this.textBox1.Text.Length - dot -1)
{
MessageBox.Show("小数最多2位");
this.textBox1.Text = last;
return;
}
}
else
{
MessageBox.Show("只能输入数字");
this.textBox1.Text = last;
return;
} }
last = this.textBox1.Text;
}如果懒的话,直接系统控件 NumericUpDown
DecimalPlaces = 2;//小数位数为2
Maximum = new decimal(999999.99);//最大数字为6位整数+2位小数的9
if (decimal.TryParse(strCharge, out number) == true)
{}
else
{}最好写在textbox的valided事件中