首页 > Windows开发 > 详细

实现:C#窗体中的文本框只能输入中文汉字,其他输入无效。问:正则表达式怎么用?

时间:2018-06-15 10:29:54      阅读:241      评论:0      收藏:0      [点我收藏+]
原文:实现:C#窗体中的文本框只能输入中文汉字,其他输入无效。问:正则表达式怎么用?

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
? ? ? ? {
? ? ? ? ? ? Regex rg = new Regex("^[\u4e00-\u9fa5]$");
? ? ? ? ? ? if (!rg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != ‘\b‘) //‘\b‘是退格键
? ? ? ? ? ? {
? ? ? ? ? ? ? ? e.Handled = true;
? ? ? ? ? ? }
? ? ? ? }
或者
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
? ? ? ? {
? ? ? ? ? ? Regex rg = new Regex("^[\u4e00-\u9fa5\b]$"); //\b是退格键
? ? ? ?if (!rg.IsMatch(e.KeyChar.ToString()))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? e.Handled = true;
? ? ? ? ? ? }
? ? ? ? }




文本框只能输入汉字,字母、数字、符号、空格键入无效,退格能用,完全实现了。非常感谢!


转自:http://zhidao.baidu.com/question/421426815.html

实现:C#窗体中的文本框只能输入中文汉字,其他输入无效。问:正则表达式怎么用?

原文:https://www.cnblogs.com/lonelyxmas/p/9185859.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!