如何限制WPF文​​本框中的特殊字符 [英] How can I restrict special character in WPF text box

查看:78
本文介绍了如何限制WPF文​​本框中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



请求您帮助限制WPF文​​本框中的特殊字符,包括shift键+键



所有正则表达式都不考虑 shiftkey +键的组合。



这就是我所说的不适用于 shiftkey 的组合



var regex = new Regex(@[^ a-zA-Z0-9 \\ \\ s]);

if(regex.IsMatch(e.Key.ToString()))

{

e.Handled =真的;

}



问候

解决方案

你为什么不呢?探索WPF TextBoxes的各种EventHandler,并拒绝你不想通过的键?



在Windows Forms中这很容易将TextBox条目限制为只有字母和数字:

  private   void  textBox1_KeyPress(对象 sender,KeyPressEventArgs e)
{
e.Handled =! Char.IsLetterOrDigit(e.KeyChar);
}

WPF的架构(附加事件,事件路由等)使这更加困难:[ ^ ]。



你可以找到一些限制密钥输入的代码示例此QA线程解决方案中的WPF TextBox:[ ^ ]。


Hi All

Request your help in restricting special character in WPF text box including "shift key" + "Keys"

All regular expressions are not considering the combination of shiftkey + keys.

This is what I've which doesn't work with the combination of shiftkey

var regex = new Regex(@"[^a-zA-Z0-9\s]");
if (regex.IsMatch(e.Key.ToString()))
{
e.Handled = true;
}

Regards

解决方案

Why don't you explore the various EventHandlers for WPF TextBoxes, and reject the keys you don't want to get through ?

It's this easy in Windows Forms to restrict TextBox entry to only letters and digits:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = ! Char.IsLetterOrDigit(e.KeyChar);
}

The architecture of WPF (Attached Events, Event Routing, etc.) makes this more difficult: [^].

You can find some code examples of restricting key-entry in the WPF TextBox in the solutions on this QA thread here: [^].


这篇关于如何限制WPF文​​本框中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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