Windowscommunitytoolkit: Add a MaskedTextBox control

Created on 18 Aug 2016  路  28Comments  路  Source: windows-toolkit/WindowsCommunityToolkit

The one was presented in WinForms but now there is no satisfactory implementation.

531 voices must be heard: https://wpdev.uservoice.com/forums/110705-universal-windows-platform/suggestions/8482078-maskedtextbox-control

controls feature help wanted in progress sdkcheck

Most helpful comment

Good suggestion

All 28 comments

Good suggestion

There is an opinion that it must be native control like TextBox because of performance.

I have a simple port of WinForms implementation but it has issues such as

  • [Password|Prompt]Char have a type of character, so devs can't set them in XAML;
  • character doubling during typing on phones;
  • not all keyboard shortcuts are supported.

So I don't know can it be the entry point or not.

But this implementation has a good advantage: it inherits TextBox so the same styles can be applied on both.

We also have an implementation that relies on a ported version of MaskedTextProvider which is almost 3000 lines long and is pretty hard to understand.
So yes, we do need MaskedTextBox from Microsoft community gurus.

Good suggestion. Not only MaskedTextBox, both MaskedPasswordBox!!!

@llvk : please provide a PR, the community can then help improving it

@h82258652 WinForms MaskedTextBox supports password mode.
@deltakosh Ok, next week may be.

@deltakosh hey, I've been busy for a long time so today is first day I could provide the PR.

No rush :)

Ok, I've started to implement this.
Let's discuss the interface: mask format, features and so on.

Do we need regex Mask? What behaviour of Prompt is implied (we can display prompt as background control, for example)?

There is a lot of questions the community should answer.

@llvk I think the message should just be text that appears like a tooltip

I would love to see mask functionality in an attached property
Example:

<TextBox uwp:TextMask.Type="Numeric">

Or

<TextBox uwp:TextMask.RegXMask="\d">

The above probably isn't valid regex.

Reason: Styling. I don't want to style two textboxes.

@ScottIsAFool I see it like placeholder.

@skendrot It's kinda complicated to implement this with default TextBox. I would prefer Windows.UI.Core.Text based implementation.

@llvk why attached property would be complex ?, I see that @skendrot suggestion make sense.
Also incase it will be a single control why don't you just extend TextBox class so the control would have all textbox features + mask :)

@llvk Sorry. I must have misunderstood the point of this (having not done an extensive code and not familiar with the WinForms control). I thought the purpose of the control was to limit text input to a certain format.

@IbraheemOsama @skendrot ok, I'll try again (the last try was a long long time ago, I don't remember the entire list of that implementation problems).

@llvk sorry for inconstancy but the control will contains a lot of code and maybe you will need something like private virtual state of control text and this won't be applicable by attached property as I know. so do you're calculations and go for what fits the control most :)

I'm aligned with @skendrot : a MaskedTextBox has to limit input to a given mask

I will start developing this control :)

If this isn't implemented in the default TextBox (and RichEditBox) as @skendrot suggests, you will lose a lot without re-implementing a bunch of other text support aspects (dictionary, suggestions, narration with accessible technologies, word selection, IME support, etc.). I would really try to find a way to implement this using TextBox as the base as you get all those services (and other ISVs have already done this).

@deltakosh tag this sdkcheck :-)

Can't agree more. (And I think that @IbraheemOsama is already implementing it as an attached property)

Yes, using attached property :)

MaskedTextBox is also something we're looking at building for our commercial UWP Editors controls. It really does need to harness the native TextBox somehow because as Tim said, you otherwise lose all kinds of features.

@timheuer - One problem we've consistently run into though when harnessing a native UWP TextBox is the ability to cancel a text change within a TextBox before it commits the value. You have a TextChanging event in the UWP TextBox, but per MSDN: "When the TextChanging event occurs, the Text property already reflects the new value (but it's not rendered in the UI)."

That's not a very useful event at all. We need to have some kind of event that allows us to cancel the text change before it ever occurs. Tell us the actual text replacement (what was deleted, what was inserted, and where, and whether it was a composition/programmatic/etc. change) and let us e.Cancel if we need to based on invalid input. That would make it relatively easy to build a control like a MaskedTextBox. There are tons of use cases for that kind of event.

In WPF, we are able to use TextBox.PreviewTextInput to block incoming input successfully, but from what we can see, there is no similar event that allows selectively blocking input in UWP. Please correct me if I'm wrong.

What about inheriting from textbox and overriding on key down? I think you could stop the propagation there

You can't do that since KeyDown doesn't map keys to actual text (VirtualKey.A vs. "A" or "a") and it also doesn't handle IME composition for Asian, etc. cultures.

What @BillHenning said :-) -- he's acutely聽aware what is needed here and the challenges of ensuring you'd get it right for all input. I don't think you're wrong on the聽general sense Bill but let me see if聽there are ways to get at the lower-level impl to maybe allow that to happen. Will report back on this thread after talking to the team.

@BillHenning you can reset text on textChanging which can simulate the cancel handle, the user will not notice that we written a character and it is removed, you can check the attached PR for the mask I created it is not final yet but you can't detect the cancel because of textChanging

I will finish it next week in case of you need it

Was this page helpful?
0 / 5 - 0 ratings