首页 > 其他 > 详细

TextBox(只允许输入字母或者数字)

时间:2016-07-12 12:08:24      阅读:136      评论:0      收藏:0      [点我收藏+]

实现如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PISS.View.CustomControl
{
public class LetterAndNum : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

SetStandard(e);
}

private void SetStandard(System.Windows.Forms.KeyPressEventArgs e)
{
//只允许输入字母
Regex regex = new Regex(@"^([A-Za-z0-9]|[\b])+$");
MatchCollection mc = regex.Matches(e.KeyChar.ToString());
foreach (Match ma in mc)
{
e.Handled = false;
return;
}

e.Handled = true;
}
}
}

TextBox(只允许输入字母或者数字)

原文:http://www.cnblogs.com/YYkun/p/5662833.html

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