WinForms MaskedTextBox to accept certain chars

大城市里の小女人 提交于 2019-12-13 18:38:26

问题


I have a MaskedTextBox that accepts alpha numeric chars but I also want to accept brackets "()". How can I change the mask to accepts brackets also?


回答1:


See this answer to a similar question:

How to set Regex Expression as the Mask for a MaskedTextBox in C#?

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.




回答2:


You can simply put this line of code into form constructor:

 maskedTextBox1.Mask = "(000)000.000";


来源:https://stackoverflow.com/questions/7159749/winforms-maskedtextbox-to-accept-certain-chars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!