Restricting characters in a TextBox
Solution 1
The simplest thing that could possibly work is to bind to the OnTextChanged
event and modify the text according to your rules.
<TextBox x:Name="TheText" TextChanged="OnTextChanged" MaxLength="4"/>
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
if (TheText.Text.Length == 0) return;
var text = TheText.Text;
int result;
var isValid = int.TryParse(text, out result);
if (isValid) return;
TheText.Text = text.Remove(text.Length - 1);
TheText.SelectionStart = text.Length;
}
However, I'd shy away from this approach since the mantra of Metro is touch first UI and you can easy do it in a touch first manner with a FlipView
control.
Solution 2
Try setting TextBox.InputScope property to InputScopeNameValue.Number, as mentioned in Guidelines and checklist for text input in MSDN.
Josh Buhler
Developer at Control4. If I'm not coding, I'm usually playing games, guitar, or out in the garage.
Updated on June 28, 2022Comments
-
Josh Buhler almost 2 years
I'm building a form in a C# WinRT app, and I'd like to restrict the characters in one of the TextBox components to numerals only. (This TextBox would be for a user to enter a year into.)
I've searched for a while, but haven't been able to figure this one out without setting up an event listener on the
TextChanged
event, and inspecting thetext
property on every key press. Is there a way to simply say that a user can only enter specific characters into a TextBox? -
Ritch Melton about 12 yearsIs there a MaskedTextBox for WinRT?
-
olatunjee about 12 years@RitchMelton I don't know presently. I guess the answer should be yes.
-
Ritch Melton about 12 yearsThe current docs say no. msdn.microsoft.com/en-us/library/windows/apps/…
-
Avner Shahar-Kashtan about 12 yearsThis is cool, and (sort-of) exists in WPF as well. Didn't know about this one. Thanks!
-
Josh Buhler about 12 yearsThis kinda works, but won't do exactly what I'm trying to accomplish. It looks like the InputScope property serves more as a hint to the system as to which soft keyboard the system should display, but it won't create a masked TextBox that limits the inputs. (Finally found this thread in the MSDN forums - link below, see post from Chipalo Street, from Thursday, March 22, 2012 5:49 PM). social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/…
-
sdb about 12 yearsAh, seems like I misunderstood the documentation. Thanks for pointing this out.
-
Josh Buhler about 12 yearsMarking this as the answer as it mostly solves the problem I was having. This will restrict the TextBox to only displaying numbers. However, I've since revisited this part of my app, and replaced the year field with a ComboBox, to sidestep the problem entirely. (I've got a fixed range of years possible, so I don't know why I didn't use a ComboBox in the first place.)
-
David Rector over 9 yearsShift-4, which should be a dollar sign, is not filtered out with this code. The 4 key is seen and accepted as a digit. See this question: stackoverflow.com/questions/13001215/… where various methods are shown for detecting the shift, control, etc., keys.