Hiding the PromptChar for .NET's MaskedTextBox

元气小坏坏 提交于 2019-12-29 08:47:50

问题


Is there a way to use MaskedTextBox without displaying the PromptChar? I would like to specify a mask to validate input against, but I don't want any prompt characters to take up space during edit mode.

The reason for this is when I want to accept free text with a limited character set, all characters are optional alphanumerics. The problem I have is that when I select all text, even the prompt characters are selected.

Setting PromptChar to (" ") doesn't work. Setting it to Zero-width space (\u200B) seems to work, but weird things happen when the mask reaches a certain length (13 characters seems to be the magic number). Or would it be best to just roll my own?


回答1:


This is an old question, but hopefully this will help others stumbling upon the same issue. For me on .net 2.0 and above there is the property, 'HidePrompt', setting this to true hides the prompt text, until you click on the field. So, first set your prompt character to a space " ", and then change the 'InsertKeyMode' to "Insert", and then set the 'HidePrompt' to True. Changing the prompt character to a space prevents the prompt from being seen when the user clicks on it, hideprompt hides it in general, and insert mode takes the user to the beginning of the field instead of allowing them to click in the middle of the prompt character field.




回答2:


How about just using a textbox and then using a regex (or some other validation process) to verify that the text is valid when the control loses focus. Alternatively, if this is a Window's app, jump on the TextChanging event (or KeyPress) and stop any invalid characters actually getting into the input in the first place.

This can be done in javascript for a web page also.




回答3:


did you check the property CutCopyMaskFormat ?

it seems that it's set to : IncludePrompt or IncludePromptAndLiterals.



来源:https://stackoverflow.com/questions/1459797/hiding-the-promptchar-for-nets-maskedtextbox

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