控制textbox中只输入数字和小数点 如何控制textbox中输入内容,只能输入数字和小数点,且小数部分后最多只能为2位,并且小数部分只能是0.25的倍数,整数部分不限制。我想用正则表达式可以实现,但我不知怎么写。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 判断输入的数字是否是数字以及包含两位小数点 /// </summary> /// <param name="strInput">输入要验证的数字</param> /// <returns>返回true,表示验证成功,false表示失败,数字的格式不正确</returns> public bool SetNumericNinePointFormat(string strInput) { bool flag = true; if (isNumericAndDecimal(strInput)) { if (strInput.IndexOf(".") > 0) { if (strInput.LastIndexOf(".") != strInput.IndexOf(".")) { flag = false; } else { if (strInput.Split('.')[1].Length > 2) { flag = false; } if (strInput.Split('.')[0].Length > 9) { flag = false; } } } else { if (strInput.Length > 8) { flag = false; } } } else { flag = false; } return flag; } private void Text1_KeyPress(object sender, KeyPressEventArgs e) { if (!((e.KeyChar >= '0') && (e.KeyChar <= '9') || e.KeyChar =='.')) { e.Handled = true; } } LZ请参考"^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1-9][0-9]*$" //正整数 "^((-\d+)|(0+))$" //非正整数(负整数 + 0) "^-[0-9]*[1-9][0-9]*$" //负整数 "^-?\d+$" //整数 "^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0) "^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数 "^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮点数(负浮点数 + 0) "^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数 "^(-?\d+)(\.\d+)?$" //浮点数 "^[A-Za-z]+$" //由26个英文字母组成的字符串 "^[A-Z]+$" //由26个英文字母的大写组成的字符串 "^[a-z]+$" //由26个英文字母的小写组成的字符串 "^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串 "^\w+$" //由数字、26个英文字母或者下划线组成的字符串 "^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址 "^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url这里也是转载其他人的,值得收藏 使用举例: private void txtequNumber_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) { Regex reg = new Regex(@"[^\d]", RegexOptions.IgnoreCase); //只能输入数字 txtequNumber.Text=reg.Replace(txtequNumber.Text,""); } Regex 需引用命名空间 using System.Text.RegularExpressions; bool lockKey=false; private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { char keyChar = e.KeyChar;//输入的字符 int value = (int)keyChar;//转换成整型 string text = textBox1.Text; if (lockKey && value != 8) { e.Handled = true; return; } if ((keyChar >= '0' && keyChar <= '9') || keyChar == '.' || value == 8)//8表示退格键 { if (text.Contains('.')) { switch (keyChar)//0.25倍数的可能组合 { case '0': textBox1.Text = text + "00"; lockKey = true; break; case '2': textBox1.Text = text + "25"; lockKey = true; break; case '5': textBox1.Text = text + "50"; lockKey = true; break; case '7': textBox1.Text = text + "75"; lockKey = true; break; default: if (value != 8) e.Handled = true; break; } } else { lockKey = false; } } else e.Handled = true; } richtextbox文本显示的优先级 请问有时这样会超时 求一个emacs.vsk文件:) 一个方法里,有个委托调用自己这个方法,该怎么理解? C#连SQL Server的问题,在线等。。。。 关于office的考题的编程思路 socket为什么不能连自己的机器? rdlc按行分组统计显示在统计列,怎么做?按组统计顺序改变不了。 程序不能运行,在其他机器上。。。 大家好:请问在C#中如何写一个最简单的控件(包括如何安装),还可以加分(要多少可以说(10-100)) c# listview和数据库之间通讯 求助,如何创建和关闭一个虚拟磁盘
/// 判断输入的数字是否是数字以及包含两位小数点
/// </summary>
/// <param name="strInput">输入要验证的数字</param>
/// <returns>返回true,表示验证成功,false表示失败,数字的格式不正确</returns>
public bool SetNumericNinePointFormat(string strInput)
{
bool flag = true; if (isNumericAndDecimal(strInput))
{ if (strInput.IndexOf(".") > 0)
{
if (strInput.LastIndexOf(".") != strInput.IndexOf("."))
{
flag = false;
}
else
{
if (strInput.Split('.')[1].Length > 2)
{
flag = false;
}
if (strInput.Split('.')[0].Length > 9)
{
flag = false;
}
}
}
else
{
if (strInput.Length > 8)
{
flag = false;
}
}
}
else
{
flag = false;
} return flag;
}
{
if (!((e.KeyChar >= '0') && (e.KeyChar <= '9') || e.KeyChar =='.'))
{
e.Handled = true;
}
}
"^\d+$" //非负整数(正整数 + 0)
"^[0-9]*[1-9][0-9]*$" //正整数
"^((-\d+)|(0+))$" //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$" //负整数
"^-?\d+$" //整数
"^\d+(\.\d+)?$" //非负浮点数(正浮点数 + 0)
"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
"^((-\d+(\.\d+)?)|(0+(\.0+)?))$" //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
"^(-?\d+)(\.\d+)?$" //浮点数
"^[A-Za-z]+$" //由26个英文字母组成的字符串
"^[A-Z]+$" //由26个英文字母的大写组成的字符串
"^[a-z]+$" //由26个英文字母的小写组成的字符串
"^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串
"^\w+$" //由数字、26个英文字母或者下划线组成的字符串
"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$" //email地址
"^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$" //url
这里也是转载其他人的,值得收藏 使用举例:
private void txtequNumber_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
Regex reg = new Regex(@"[^\d]", RegexOptions.IgnoreCase); //只能输入数字
txtequNumber.Text=reg.Replace(txtequNumber.Text,"");
}
Regex 需引用命名空间 using System.Text.RegularExpressions;
bool lockKey=false; private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char keyChar = e.KeyChar;//输入的字符
int value = (int)keyChar;//转换成整型
string text = textBox1.Text; if (lockKey && value != 8)
{
e.Handled = true;
return;
} if ((keyChar >= '0' && keyChar <= '9') || keyChar == '.' || value == 8)//8表示退格键
{
if (text.Contains('.'))
{
switch (keyChar)//0.25倍数的可能组合
{
case '0':
textBox1.Text = text + "00";
lockKey = true;
break;
case '2':
textBox1.Text = text + "25";
lockKey = true;
break;
case '5':
textBox1.Text = text + "50";
lockKey = true;
break;
case '7':
textBox1.Text = text + "75";
lockKey = true;
break;
default:
if (value != 8)
e.Handled = true;
break;
}
}
else
{
lockKey = false;
}
}
else
e.Handled = true;
}