C#的TextBox控件输入测试
只允许输入数字的测试:
代码如下: (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;
}
}
}
本文介绍如何使用C#编程语言实现TextBox控件仅接受数字输入的功能,通过正则表达式验证输入字符,确保只能输入整数或浮点数,并且详细展示了具体的代码实现。
750

被折叠的 条评论
为什么被折叠?



