Garry Wong
Garry Wong

Reputation: 823

Why doesn't my Mask on a MaskedTextBox work?

I want a mask that will enforce an input of digit:digit, with additional optional digits on either side. Examples:
1:2
123:1234

So the mask I enter is
099\:0999
I expect this is supposed to mean "A required digit, followed by an two optional digits, followed by literal :, followed by a required digit, followed by any three optional digits".

However upon testing the mask, the program happily accepts space in place of digits, so I am easily capable of ending up with something like :2, for example. The MaskInputRejected Event doesn't get raised either.

Am I doing something wrong? I understand that for 9, a space is valid input, but for 0, I expect a digit is required, and space is not valid.

Upvotes: 0

Views: 109

Answers (1)

lordjeb
lordjeb

Reputation: 1306

This seems to be either a bug in the control or an error in the documentation. The 0 in the mask will still allow spaces. Here is an article that will help you prevent the user from entering spaces.

MSDN: How to disable the space bar in maskedTextBox?

And the relevant code is...

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == ' ' ) e.KeyChar = (char)0;
}

Upvotes: 1

Related Questions