码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-07-12 12:10:42      阅读:117      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!