LightSwitch Only Numbers in TextBoxes

How to allow only numeric keystrokes (numbers 0-9) in a TextBox?

Answer:

Use the KeyDown event and e.Handled property.

Example:

Imagine you have a NewClient screen and only want numbers to be entered in a property called “Code”.

In NewClient.cs

partial void NewClient_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService> saveChangesTo)
{
   this.FindControl("Code").ControlAvailable += TextBoxAvailable;
   // Write your code here.
   this.ClienteProperty = new Cliente();
}

private void TextBoxAvailable(object sender, ControlAvailableEventArgs e)
{
   ((System.Windows.Controls.TextBox)e.Control).KeyDown += TextBoxKeyDown;
}

private void TextBoxKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
   // Determine whether the keystroke is a number from the top of the keyboard.
   if (e.Key < System.Windows.Input.Key.D0 || e.Key > System.Windows.Input.Key.D9)
   {
      // Determine whether the keystroke is a number from the keypad.
      if (e.Key < System.Windows.Input.Key.NumPad0 || e.Key > System.Windows.Input.Key.NumPad9)
      {
         // Determine whether the keystroke is a backspace.
         if (e.Key == System.Windows.Input.Key.Back || e.Key == System.Windows.Input.Key.Tab)
         {
            // OK
         }
         else
         {
            // A non-numerical keystroke was pressed.
            e.Handled = true;
         }
      }
   }
}

The code validates you enter only digits (numbers) but also allows the TAB key and the Backspace key. Otherwise the keystroke is canceled with e.Handled = true. It works as you enter the keystrokes not after the fact when you already have invalid text inserted.

I hope this helps you.

Update: JamesRpo told me that if you want to prevent people from entering the symbols that correspond to the numbers on top of the keyboard (not the keypad numbers.) How you can do that? I’m really happy he asked because at the moment I didn’t know the answer, it took me like 15 minutes to find a solution and for a moment I thought maybe there is no solution. So thank you.

In that case you need to use the static class Keyboard and the Modifiers property. As you can see in the following image you can check for the key Alt, Apple, Control, None, Shift and Windows.

modifierkeys

Don’t forget to add a using to System.Windows.Input the code could be something like this:

private void TextBoxKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
// Check if the Modifier Key SHIFT is pressed.
if (e.Key > System.Windows.Input.Key.D0 && e.Key < System.Windows.Input.Key.D9 && (Keyboard.Modifiers & ModifierKeys.Shift) == 0)
            {
            }
            else
            {
                // A non-numerical keystroke was pressed.
                e.Handled = true;
            }
}
Advertisements
This entry was posted in LightSwitch and tagged , , , , , , , , . Bookmark the permalink.

3 Responses to LightSwitch Only Numbers in TextBoxes

  1. JamesRpo says:

    Do you know any way to prevent people from entering special characters?? with the code above users can still press shit + (0-9) from the top and get all special characters.

    • pepitosolis says:

      Thank you for your question and sorry for taking me too long to answer.
      Don’t get me wrong about my code, I wanted to allow those keystrokes because it was for a phone number and well they wanted some symbols.

      Imagine that you only want the keys on top of the keyboard.
      Remember to add this using:

      using System.Windows.Input;

      private void TextBoxKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      {
      if (e.Key > System.Windows.Input.Key.D0 && e.Key < System.Windows.Input.Key.D9 && (Keyboard.Modifiers & ModifierKeys.Shift) == 0)
      {
      }
      else
      {
      // A non-numerical keystroke was pressed.
      e.Handled = true;
      }
      }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s