Character restriction in TextBox

I am creating a form in a C # WinRT application, and I would like to limit the characters in one of the TextBox components to only numbers. (This TextBox will be for the user to enter the year in.)

I searched for a while, but couldn't figure it out without setting up an event listener in the TextChanged event and checking the text property each time I press a key. Is there a way to simply say that a user can only enter certain characters in a TextBox?

+6
source share
6 answers

The simplest task that can work is to bind to the OnTextChanged event and change 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 would shy away from this approach, as the Metro mantra touched the first user interface, and you can easily do it with one touch using FlipView .

+8
source

Try setting the TextBox.InputScope property to InputScopeNameValue.Number, as described in the Manuals and Checklist for entering text on MSDN.

+6
source

Valid year

 DateTime newDate; var validYear = DateTime.TryParseExact("2012", "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out newDate); //valid 

Invalid Year

 var validYear = DateTime.TryParseExact("0000", "yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out newDate); //invalid 
0
source

This seems to work for me:

  private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e) { if ((e.Key < VirtualKey.Number0) || (e.Key > VirtualKey.Number9)) { // If it not a numeric character, prevent the TextBox from handling the keystroke e.Handled = true; } } 

For all values, see the documentation for the VirtualKey listing .

0
source

Based on the publication in the link by adding a tab to enable navigation.

 private void decimalTextBox_KeyDown(object sender, KeyRoutedEventArgs e) { bool isGoodData; // flag to make the flow clearer. TextBox theTextBox = (TextBox)sender; // the sender is a textbox if (e.Key>= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) // allow digits isGoodData = true; else if (e.Key == Windows.System.VirtualKey.Tab) isGoodData = true; else if (e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9) // allow digits isGoodData = true; else if (e.Key == Windows.System.VirtualKey.Decimal || (int)e.Key == 190) // character is a decimal point, 190 is the keyboard period code // which is not in the VirtualKey enumeration { if (theTextBox.Text.Contains(".")) // search for a current point isGoodData = false; // only 1 decimal point allowed else isGoodData = true; // this is the only one. } else if (e.Key == Windows.System.VirtualKey.Back) // allow backspace isGoodData = true; else isGoodData = false; // everything else is bad if (!isGoodData) // mark bad data as handled e.Handled = true; } 
0
source

Use the MaskedTextBox control. For numbers only, use the Mask property to specify the characters and length, if any. for example, if you want to enter only five numbers, you set the mask property to "00000". Just like that. Windows handles the restriction for you.

-2
source

Source: https://habr.com/ru/post/913675/


All Articles