Restricting characters in a TextBox

11,076

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.

Share:
11,076
Josh Buhler
Author by

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, 2022

Comments

  • Josh Buhler
    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 the text 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
    Ritch Melton about 12 years
    Is there a MaskedTextBox for WinRT?
  • olatunjee
    olatunjee about 12 years
    @RitchMelton I don't know presently. I guess the answer should be yes.
  • Ritch Melton
    Ritch Melton about 12 years
  • Avner Shahar-Kashtan
    Avner Shahar-Kashtan about 12 years
    This is cool, and (sort-of) exists in WPF as well. Didn't know about this one. Thanks!
  • Josh Buhler
    Josh Buhler about 12 years
    This 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/thr‌​ead/…
  • sdb
    sdb about 12 years
    Ah, seems like I misunderstood the documentation. Thanks for pointing this out.
  • Josh Buhler
    Josh Buhler about 12 years
    Marking 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
    David Rector over 9 years
    Shift-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.