MaskedTextBox not handling non-matching text updates correctly

Editors for WPF Forum

Posted 6 years ago by Ryan Courreges
Version: 17.2.0662
Platform: .NET 4.7
Environment: Windows 10 (64-bit)
Avatar

Here's an example to illustrate the issue I found. Using a MaskedTextBox with the following mask:

[^0-9\s][^\s]{0,29}

so the input can't start with a number or whitespace and the rest of the value can't contain whitespace up to a total of 30 characters. This is great if you are typing from the beginning or appending to the end.

However, if the user attempts to input a space into the middle of the value somewhere, e.g., attempt to change "myValue" to "my Value", debugging the code shows me that the call to match matches the text up to the invalid character and gets rid of everything after, leaving me with "my". That's all fine and dandy and the matcher is just doing what it's told. 

Now to fix this to behave how any input should behave, just block that input and leave the original value intact, I would need to use the begin and end anchors so my mask would become:

^[^0-9\s][^\s]{0,29}$

The problem is, the implementation doesn't support these Regex elements.

 

So, the code should be updated to have a property to set it to match the whole string to give the proper behavior or the begin and end anchors need to be supported which would accomplish the same thing. (There may also be some regex kung fu that could be applied, but I'm not a regex expert). Is this something that could be tackled in an upcoming release? Or has it already been fixed in 663 or 664?

Comments (3)

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ryan,

Thanks for writing about this scenario.  It's a bit tricky to block some of this since we only know the old/new text string and selection.  So we sometimes have to make assumptions on what the user is doing only based on those values.  I think we were able to add some criteria that will block the input if the user appears to be adding text with no selection but the match gets trimmed at the original selection start.  That seems to resolve this issue.  Would you like to try a preview build to help test it out?


Actipro Software Support

Posted 6 years ago by Ryan Courreges
Avatar

I can see that being tricky. Yes, I'd love to try out a preview build.

Posted 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ryan,

Please write our support address and mention this thread and I'll send you a URL.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.