Use the numeric keypad decimal key as the numeric separator for all locales as in Excel

by Sébastien Van Bruaene 29. April 2009 04:15

If the locale specifies that the numeric separator is something else then '.' (e.g. ',' in Belgium) then the numeric keypad decimal key can not be used to enter a numeric separator in a WPF input control. However in applications like Excel this works. How can we do this in a WPF input control ? Very simple: we handle the PreviewKeyDown event and emulate the input of a numeric separator for that locale.

private static void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)

{

    if(e.Key == Key.Decimal &&

        NumberFormatInfo.CurrentInfo.NumberDecimalSeparator != ".")

    {

        var _this = (sender as TextBox);

        if(_this != null)

        {

            e.Handled = true;

 

            var eventArgs = new TextCompositionEventArgs(

                Keyboard.PrimaryDevice,

                new TextComposition(

                    InputManager.Current,

                    Keyboard.FocusedElement,

                    NumberFormatInfo.CurrentInfo.NumberDecimalSeparator))

                    {

                        RoutedEvent = ContentElement.TextInputEvent

                    };

 

            InputManager.Current.ProcessInput(eventArgs);

        }

    }

}

 

Currently rated 4.5 by 2 people

  • Currently 4.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

Comments

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

I am a software engineer / architect who spends most of his time working with Microsoft technologies (.NET / WPF / EF / SQL Server). I have worked in the healthcare sector for some time but nowadays I work for a great company called Metanous. 

I am very happily married but I have no children yet. For now I only have 4 rabbits and 4 chickens. My rabbits are of the "Vlaamse reus" breed, the biggest kind of rabbit in the world :)

In my spare time I play a little basketball and mini soccer (yes that is an actual sport).