如何设置正则表达式表达的面具在C#中的MaskedTextBox中? [英] How to set Regex Expression as the Mask for a MaskedTextBox in C#?

查看:595
本文介绍了如何设置正则表达式表达的面具在C#中的MaskedTextBox中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从正则表达式的表达式中设置MaskedTextBox中的面具。比如我希望有一个有效的电子邮件,十进制值和其它正则表达式表达反对MaskedTextBox中。

I want to set the Mask of MaskedtextBox from Regex expression. Such as i want a valid email, Decimal values and other regex expressions against a MaskedtextBox.

推荐答案

有关参考,这说明你可以用做面膜:
HTTP ://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask(VS.90)的.aspx

For reference, this describes what you can do with a mask: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask(VS.90).aspx

我可以看到你需要设置从正则表达式面具的唯一的一次是,如果你没有对正则表达式的控制,例如,如果它是从用户还是从数据库中获得的。面具是比正则表达式不同的格式,并且不那么强大。所以,在某些时候,甚至都不能够做。据我所知,你根本无法做到像带着面具的电子邮件验证,由于各部分的变化的位置和长度。

The only time I could see you needing to set the mask from a regular expression is if you don't have control over the regular expression, for example if it's acquired from the user or from a database. Masks are in a different format than regular expressions and aren't as powerful. So some of the time, it might not even be able to be done. As far as I know, you simply can't do validation of something like an email with a mask, because the position and length of the various parts changes.

而不是使用面具,你应该只使用正规验证,然后就可以直接使用正则表达式。确保(正常,非屏蔽)文本框的CausesValidation属性为true,则拦截验证事件,如果正则表达式不匹配,设置CancelEventArgs.Cancel为true。

Instead of using a mask, you should probably just use regular validation, and then you can use the regular expressions directly. Make sure that the CausesValidation property of the (regular, not masked) textbox is true, then intercept the Validating event and if the regular expression doesn't match, set the CancelEventArgs.Cancel to true.

这篇关于如何设置正则表达式表达的面具在C#中的MaskedTextBox中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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