您的位置:首页 > 编程语言 > C#

C#的TextBox控件输入测试-只允许输入数字的测试:

2008-03-31 14:38 423 查看
代码如下: (VS2005)

public TextBoxInputCheck(object sender, KeyPressEventArgs e,INPUTTYPE type)
{
if(type == INPUTTYPE.INT)
{
string pattern = @"^[0-9]";
Regex reg = new Regex(pattern);
if ((!reg.Match(e.KeyChar.ToString()).Success) && (e.KeyChar.ToString() != ""))
{
e.Handled = true;
}
}
else if(type == INPUTTYPE.FLT)
{
string pattern = @"^[0-9]|.$";
Regex reg = new Regex(pattern);
if ((!reg.Match(e.KeyChar.ToString()).Success) && (e.KeyChar.ToString() != ""))
{
e.Handled = true;
}
else if (e.KeyChar.ToString() == "." && (sender as TextBox).Text.IndexOf('.') > 0)
{
e.Handled = true;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: