Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have a issue which need everyone's help.

I have C# WPF project which run in tablet.
my textboxs only allow to input numberic and I did this on English keyboard,
but when I change to japanese keyboard in tablet, I can input japanese character.

When change to japanese keyboard, I want to only input numberic and con't allow to input japanese character.

could you help me this?

thanks in advance
Posted

1 solution

If I can assume that the input goes in TextBox, you can always filter out unwanted characters.

You need to use the event PreviewTextInput, which you can cancel. For example
C#
using System.Windows;

// ...

static bool IsNumericOnly(string value) {
    foreach(char digit in value)
        if (!char.IsDigit(digit))
            return false;
    return true;
} //IsNumericOnly
// you can use something more complicated instead,
// for example, Regex

// ...

textBox.PreviewTextInput += (sender, eventArgs) => {
    eventArgs.Handled = !IsNumericOnly(eventArgs.Text);
};

—SA
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 11-Jul-15 23:15pm    
Thank you for your comment on my (totally stupid and vague) post, I understand handling event should have also been posted because OP might not have known how to write it.

I will keep it in mind, I wonder how it slipped off my mind. :-)

4ed; also there was a problem that he was encountering, he needs to check for Japanese characters also, which IsDigit won't be able to solve. That is why, I used IsNumber which checks whether character is a character in Unicode. I hope that might be the reason why his code doesn't work in Japanese culture but does in English.
Sergey Alexandrovich Kryukov 11-Jul-15 23:31pm    
Thank you Afzaal. And I really appreciate your constructive response to criticism; this has always been one of the key to success.

I don't really know how IsDigit behaves in the cultures with different numerical characters (normally they are additional to globally used Western ones, such as Eastern Arabic or Indian numerals). I suspect that this function only accounts for Western digits in all cultures. Nevertheless, my code sample is only the example of how input filtering is done.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900