MaskedTextBox: Mask moves on space or char after clearing.

Pete (BSC)

Well-known member
Joined
Oct 19, 2007
Messages
89
Programming Experience
10+
I have a derived user control that inherits the MaskedTextBox... If I press the ESC key while the maskedtextbox has focus (in my code causes the .ClearValue method to be called) when I input space or a valid character, the entire mask is moved.

For example: if my mask is 00-LLL-000 with the prompt char = "#"

Before: [##-###-##]
After: [ ##-###-##] or it could be at a different position [##- ###-##]

Then, after the 2nd inputed character... it drops the original space or character and the mask is no longer moving.

If I press a button that calls .ClearValue, I don't have this problem.

I do have this code in place:
If e.KeyChar = ChrW(Keys.Escape) Then
e.Handled = True
End If

To prevent beeping.
 
Last edited:
That's the bug.

The moving part is the "bug". I don't want the mask to move.

I did "hack" a solution... the ESC key was being captured by the control which caused the char to be in front of the mask and if I hit BACKSPACE and removed it before typing, it functioned correctly.

So, I catch the ESC on the keypress and send a BACKSPACE to remove it.

Pete
 
Why not override the KeyUp event in your control and if it's the escape key, set e.Handled = True otherwise pass it on to the base?
 
You could even make a property that when set to true the escape key would clear the contents of the MTB, if set to false the escape key isn't looked at (when pressed) and is passed to the base
 
Opened up...

Now that I know what the cause of the issue is, it opens up a slew of solutions.

Both are very good suggestions, I think I'm going to go with the 1st one, override the KeyPress event with my.

The funny thing was... I would display the value of the text property and on the screen it was:
[1##/##/###]

But, in the immediate window it was
[##/##/####]

It didn't register that the "1" was even there.
 
If you're inheriting the MTB control in the first place, I would build the escape key functionality into your control instead of adding it outside of your control

Here's what I'm talking about
 
On the same page...

See attached, we are on the same page.

Even if I have e.Handled = True, it still does it.

I don't need the property "UseEscape" as this needs to function, ESC = clear value from all controls on screen.

I appreciate your sample, it reaffirms what I'm doing!
Thanks,
Pete
 

Attachments

  • MaskIssue.pdf
    12 KB · Views: 40
Interesting, with my example everything works perfectly inside the control itself, you must be doing something different which is causing the trouble, another difference I'm noticing is that I'm using the KeyUp event and you're using the KeyDown event. Also instead of MyBase.Clear use: Me.Clear

Also 'ESC = clear value from all controls on screen.'

You can use the KeyUp event outside of the control (like you would a normal textbox/maskedtextbox) if the KeyCode = Keys.Escape to call a sub that clears data from all controls in all containers on the window.
 
The other "variable" is... using Data Binding.

I created a test project (similar to what you posted) and I wasn't able to reproduce either.

Yes, I have a class that handles all the controls.
I used .Clear to try to clear the the "ESC" issue.

At this point, I'm past this issue.
Thanks for your help.
 
Back
Top