locked
How to filter key input coming into controls on a page RRS feed

  • Question

  • I would like to allow only a subset of keystrokes to be entered into text box controls, on a page in my app. I've not been able to find how this can be done in Windows Store apps. Thanks.
    • Edited by PDoug Wednesday, November 20, 2013 6:19 PM
    Wednesday, November 20, 2013 6:19 PM

Answers

  • You could follow an approach similar to this: http://www.rhyous.com/2010/06/18/how-to-limit-or-prevent-characters-in-a-textbox-in-csharp/


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by PDoug Thursday, November 21, 2013 9:54 PM
    Thursday, November 21, 2013 1:33 AM
    Moderator
  • Thanks for the web page you provided. I was able to use it to figure out how to regulate what text is accepted into a text box, from the text box's KeyDown event handler, using the Handled property of the e (KeyRoutedEventArgs) parameter, of the event Handler.

            private void AddressCanvas_Address2_textbox_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                    int PressedKeyValue = (int)e.Key;

                    // If the pressed key is not for characters A - Z / a - z, disallow entry into text box              
                    if (!(PressedKeyValue >= 65 && PressedKeyValue <= 90))
                        e.Handled = true;
            }


    • Marked as answer by PDoug Thursday, November 21, 2013 9:54 PM
    • Edited by PDoug Thursday, November 21, 2013 10:18 PM
    Thursday, November 21, 2013 9:54 PM

All replies

  • You could follow an approach similar to this: http://www.rhyous.com/2010/06/18/how-to-limit-or-prevent-characters-in-a-textbox-in-csharp/


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by PDoug Thursday, November 21, 2013 9:54 PM
    Thursday, November 21, 2013 1:33 AM
    Moderator
  • Thanks for the web page you provided. I was able to use it to figure out how to regulate what text is accepted into a text box, from the text box's KeyDown event handler, using the Handled property of the e (KeyRoutedEventArgs) parameter, of the event Handler.

            private void AddressCanvas_Address2_textbox_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                    int PressedKeyValue = (int)e.Key;

                    // If the pressed key is not for characters A - Z / a - z, disallow entry into text box              
                    if (!(PressedKeyValue >= 65 && PressedKeyValue <= 90))
                        e.Handled = true;
            }


    • Marked as answer by PDoug Thursday, November 21, 2013 9:54 PM
    • Edited by PDoug Thursday, November 21, 2013 10:18 PM
    Thursday, November 21, 2013 9:54 PM
  • One other thing I'd like to do, is modify text which is entered into the text box. Specifically I would like to ensure that all letters are entered upper case. When I try to accomplish this using the following code, the cursor in the text box is reset to the beginning of the text box, whenever a lower case letter entered into the text box is converted.

            private void AddressCanvas_Address2_textbox_TextChanged(object sender, TextChangedEventArgs e)
            {
                var MyTextbox = (TextBox)sender;

                MyTextbox.Text = MyTextbox.Text.ToUpper();
               
            }

    Can you suggest a solution to my problem? Is there a way to let the cursor remain in the same position, after the text is converted? Thank you.

    Thursday, November 21, 2013 10:14 PM