如何只允许在平板电脑中使用C#WPF输入数字 [英] how to only allow to input numberic using C# WPF in tablet

查看:90
本文介绍了如何只允许在平板电脑中使用C#WPF输入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个需要大家帮忙的问题。



我有C#在平板电脑上运行的WPF项目。

我的文本框只允许输入数字,我在英文键盘上做了这个,

但是当我换成平板电脑的日文键盘时,我可以输入日文字符。



当改为日文键盘时,我想只输入数字并且不允许输入日文字符。



你能帮帮我吗?



提前谢谢

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

推荐答案

如果我可以假设输入进入 TextBox ,你总是可以过滤掉不需要的字符。



你需要使用事件 PreviewTextInput ,您可以取消。例如

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
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


这篇关于如何只允许在平板电脑中使用C#WPF输入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆