I have a form that is dynamically created with a number of maskedtextboxes added to and their properties set (as in the code below).
If 10 characters are required the code needs to validate if the entered text length is 10. But the user is allowed to enter spaces at the beginning or end to make up those 10 characters.
The maskedtextbox doesn't seem to keep those, or at least the way I have the properties set up it doesn't keep them (maybe b/c I have 'C' set?)
I might just have something incorrectly set in the properties ... like with AllowPromptAsInput
or TextMaskFormat
or some other property, to allow this.
Also I don't want to use a space as the 'PromptChar' since I want the user to see the number of chars to enter.
private void SetupMaskedTextBox(int allowedChars)
{
const char promptCharSquare = '\u25A1';
maskedTextBox.PromptChar = promptCharSquare;
string msk = null;
for (int i = 0; i < allowedChars; i++) {
msk += "C";
}
maskedTextBox.Mask = msk;
maskedTextBox.AllowPromptAsInput = true;
maskedTextBox.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
}
You need this line:
maskedTextBox.ResetOnSpace = false;
This is a bit of an odd feature. MSDN says to use:
true if the space input character causes the current editable position in the mask to be reset; otherwise, false to indicate that it is to be processed as a normal input character. The default is true.