验证文本框窗口表单 [英] Validating a Textbox window form

查看:76
本文介绍了验证文本框窗口表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在尝试验证文本框以仅接受指定范围内的数字,但是它不起作用,不知道该怎么办..在此先感谢

解决方案

使用正则表达式:

 字符串 num = " ;

正则表达式模式=  Regex( @" );

如果(pattern.Match(num).Success)
{
    Console.WriteLine(" );
} 



上面的代码将验证大于0且小于1的十进制数字,最多3个小数位.这是在WinForms中执行的操作:

 私有 无效 KeyPress(对象发​​件人,KeyPressEventArgs e)
{
    如果(!char.IsDigit(e.KeyChar))
        e.Handled =  true ;
} 



可能会在按下非数字键时发出提示音或其他操作:请参阅 http://msdn. microsoft.com/en-us/library/h62wtc8c.aspx [ ^ ]


Hi guys i am trying to validate a textbox to accecpt only numbers within a specified range, but it is not working, any idea what to do.. Thanks in advance

解决方案

Use regular expression:

string num = "0.7";

Regex pattern = new Regex(@"^[0]+(.[0-9]{1,2})?


"); if (pattern.Match(num).Success) { Console.WriteLine("OK"); }



The above code will validate decimal numbers greater than 0 and less than 1, upto 3 decimal places.


Easier just to prevent any but numeric keys in the KeyPress event for the textbox. This is how you do it in WinForms:

private void KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar))
        e.Handled = true;
}



May want to do a beep or something when an non-digit key is pressed: See http://msdn.microsoft.com/en-us/library/h62wtc8c.aspx[^]


这篇关于验证文本框窗口表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆