Jeremy
Jeremy

Reputation: 1168

Hiding the PromptChar for .NET's MaskedTextBox

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?

Upvotes: 5

Views: 13255

Answers (3)

Jarrod Christman
Jarrod Christman

Reputation: 390

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.

Upvotes: 14

manji
manji

Reputation: 47978

did you check the property CutCopyMaskFormat ?

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

Upvotes: 0

Lazarus
Lazarus

Reputation: 43094

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.

Upvotes: 1

Related Questions