这是一个自定义控件,继承了TextBox,在TextBox基础上添加了4个属性(下载):

1.ControlType    文本框需要验证的类型

2.ControlTypeText  显示的文字(只读)

3.IsNULL      填写的内容是否可空

4.IsPass      格式是否正确(在文本框失去焦点时验证,只读)

代码如下:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Text.RegularExpressions; namespace TextBoxEx
{
[System.Drawing.ToolboxBitmap(typeof(TextBox))]
public class MyTextBox:TextBox
{
public MyTextBox() : base() { }
//类型枚举
public enum RegexType
{
Custom, //正常
Number, //数字 整数或者小数
CNString, //汉字
Zip, //邮政编码
Email, //电子邮箱
Phone, //手机号码
Integer, //整数
NInteger, //负整数
Float, //浮点数
ENChar, //英文字符
NumChar, //数字和英文字母
NumLineChar, //数字、英文字母或下划线
Url,
QQ,
DCard, //身份证
IP,
DateTime, //日期时间
Date, //日期
Year,
Month,
Day,
Time,
} #region 私有属性 /// 文本框类型
private RegexType _controlType;
//显示的名称
private string _controlTypeText = "默认";
//是否可空 默认为可空
private bool _isNULL=true;
//验证是否通过
private bool _isPass=false;
#endregion #region Properties 属性栏添加的属性 [DefaultValue(RegexType.Custom), Description("文本框类型")]
public RegexType ControlType
{
get { return _controlType; }
set
{
_controlType = value;
//对应显示的文字
this.ShowDescription(value);
//重新绘制控件
base.Invalidate();
}
} [DefaultValue("默认"), Description("控件验证描述")]
public string ControlTypeText
{
get { return _controlTypeText; }
} [DefaultValue(typeof(bool), "True"), Description("内容是否可空")]
public bool IsNULL
{
get { return _isNULL; }
set { _isNULL = value; base.Invalidate(); }
}
[DefaultValue(typeof(bool), "False"), Description("填写的内容格式是否正确,只读")]
public bool IsPass
{
get { return _isPass; }
}
#endregion
//判断验证类型
private void Testing(RegexType value, string text)
{
//可空 验证字符串为空
if (_isNULL && string.IsNullOrEmpty(text.Trim()))
{
_isPass = true;
return;
}
//不能为空 验证字符串为空
if (!_isNULL&&string.IsNullOrEmpty(text))
{
_isPass = false;
return;
}
//其他的两种情况都需要正则验证
switch (value)
{
case RegexType.Custom:
_isPass = true;
break;
case RegexType.Number:
_isPass = Proving(text, @"^-?[0-9]+\.{0,1}[0-9]*$");
break;
case RegexType.CNString:
_isPass=Proving(text,@"^[\u4e00-\u9fa5]*$");
break;
case RegexType.Zip:
_isPass = Proving(text, @"^[1-9]\d{5}$");
break;
case RegexType.Email:
_isPass = Proving(text, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
break;
case RegexType.Phone:
_isPass = Proving(text, @"^1[2-8]{2}\d{8}$");
break;
case RegexType.Integer:
_isPass = Proving(text, @"^-?[1-9]\d*$");
break;
case RegexType.NInteger:
_isPass = Proving(text, @"^-[1-9]\d*$");
break;
case RegexType.Float:
_isPass = Proving(text, @"^(-?\d+)(\.\d+)?$");
break;
case RegexType.ENChar:
_isPass = Proving(text, @"^[A-Za-z]+$");
break;
case RegexType.NumChar:
_isPass = Proving(text, @"^[A-Za-z0-9]+$");
break;
case RegexType.NumLineChar:
_isPass = Proving(text, @"^[A-Za-z0-9_]+$");
break;
case RegexType.Url:
_isPass = Proving(text, @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$");
break;
case RegexType.QQ:
_isPass = Proving(text, @"^[1-9][0-9]{4,}$");
break;
case RegexType.DCard:
_isPass = Proving(text, @"^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$");
break;
case RegexType.IP:
_isPass = Proving(text, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
break;
case RegexType.DateTime:
_isPass = Proving(text, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
break;
case RegexType.Date:
_isPass = Proving(text, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
break;
case RegexType.Year:
_isPass = Proving(text, @"^[1-9]\d{3}$");
break;
case RegexType.Month:
_isPass = Proving(text, @"^(0?[123456789]|1[012])$");
break;
case RegexType.Day:
_isPass = Proving(text, @"^(0?[1-9]|[12]\d|3[01])$");
break;
case RegexType.Time:
_isPass = Proving(text, @"^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$");
break;
default:
break;
} }
//格式是否正确
private bool Proving(string str, string regexStr)
{
Regex regex;
try
{
regex = new Regex(regexStr);
}
catch
{
return false;
}
return regex.IsMatch(str);
}
//重写了文本框的失去焦点事件
protected override void OnLeave(EventArgs e)
{
Testing(this.ControlType,this.Text);
base.OnLeave(e);
}
//验证类型的改变 对应改变显示的汉字
private void ShowDescription(RegexType value)
{
switch (value)
{
case RegexType.Custom:
this._controlTypeText = "默认";
break;
case RegexType.Number:
this._controlTypeText = "数字";
break;
case RegexType.CNString:
this._controlTypeText = "汉字";
break;
case RegexType.Zip:
this._controlTypeText = "邮政编码";
break;
case RegexType.Email:
this._controlTypeText = "电子邮件";
break;
case RegexType.Phone:
this._controlTypeText = "手机号";
break;
case RegexType.Integer:
this._controlTypeText = "整数";
break;
case RegexType.NInteger:
this._controlTypeText = "负整数";
break;
case RegexType.Float:
this._controlTypeText = "浮点数";
break;
case RegexType.ENChar:
this._controlTypeText = "英文字符";
break;
case RegexType.NumChar:
this._controlTypeText = "数字和英文字母";
break;
case RegexType.NumLineChar:
this._controlTypeText = "数字、英文字母或下划线";
break;
case RegexType.Url:
this._controlTypeText = "URL";
break;
case RegexType.QQ:
this._controlTypeText = "QQ";
break;
case RegexType.DCard:
this._controlTypeText = "身份证";
break;
case RegexType.IP:
this._controlTypeText = "IP";
break;
case RegexType.DateTime:
this._controlTypeText = "年-月-日 时:分:秒";
break;
case RegexType.Date:
this._controlTypeText = "年-月-日";
break;
case RegexType.Year:
this._controlTypeText = "年份";
break;
case RegexType.Month:
this._controlTypeText = "月份";
break;
case RegexType.Day:
this._controlTypeText = "日期";
break;
case RegexType.Time:
this._controlTypeText = "时:分:秒";
break;
default:
break;
}
}
}
}

最后生成一个dll,在左侧文本框中点反键-》选择项-》浏览,选中生成的dll文件就可以了(添加引用),像平常的TextBox一样的用法,从工具栏拖入界面,然后设置属性,最后判断IsPass。

如果有需要添加的格式验证,需在代码中添加:

1.RegexType类型枚举中添加类型

2.Testing方法中添加正则表达式

3.ShowDescription方法中添加选择验证类型对应显示的汉字

WinForm 文本框验证的更多相关文章

  1. 记录Js 文本框验证 与 IE兼容性

    最近的日常就是将测试小姐姐提交的bug进行修改,想来这种事情还是比较好开展的,毕竟此项目已上线一年多,现在只是一些前端的问题需要改正.实际上手的时候并不是这样,原项目是在谷歌上运行,后来由于要新增一个 ...

  2. js实现文本框验证和实现小数的加减乘除

    <script type="text/javascript"> //加法 var m=accAdd(1.22,1.22); //减法 var m1=accSub(1.2 ...

  3. js文本框验证

    1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...

  4. Axure文本框验证和外部url的调用

    文本框的验证和外部url的调用: 场景: 当输入文本框中的内容是满足下面条件时:输入4-10的数字,页面会跳转到QQ注册(https://ssl.zc.qq.com/v3/index-chs.html ...

  5. Winform文本框只能输入限定的文本

    比如WInform中的文本框只能输入数字活着字母和退格键,e.kaychar(按下键盘的值)

  6. JS的文本框验证以及form表单的提交阻止

    js: 1.只能输入数字 只能输入数字:<input type="text" onkeyup="javascript:ReNumber(this)" /& ...

  7. c# winform文本框数字,数值校验

    文本框数字,数值校验 public void DigitCheck_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.I ...

  8. 22.文本框验证和外部url的调用

    面板可以右键固定到浏览器并且横向纵向都剧中 如果要在图片上进行点击或者其他操作 可以覆盖一个图片热区或者矩形(透明的)充当一个按钮的操作 这个提示的图片是默认隐藏的 通过右上角那个隐藏的勾 文本框右边 ...

  9. C# Winform 文本框默认提示信息

    private string Notes = "提示文本"; private void textBox1_Leave(object sender, EventArgs e) { / ...

随机推荐

  1. android ListView子布局中按钮响应点击事件

    只需要在子布局的根布局中添加以下属性即可: android:descendantFocusability="blocksDescendants"

  2. python学习笔记4-redis multi watch实现锁库存

    python 关于redis的基本操作网上已经很多了,这里主要介绍点个人觉得有意思的内容1.redis的事务操作以及watch 乐观锁:后面描述2.tornado下异步使用redis的方式       ...

  3. appium远程调用appium server

    如何通过本地的代码,调用远程的server呢?   例如:我有两台电脑A(192.168.112.10)和B(192.168.112.11),那我怎么能在A执行本地脚本,但是使用B上的server呢? ...

  4. perl中常见的语法规则和函数

    数值比较操作符         字符串 相等          ==                        eq 不等          !=                         ...

  5. SQL联合查询两个表的数据

    刚有个项目,需要查询水位数据表中的水位信息,及查询降雨量表中统计时段降雨量的数据,以计算出日降雨量,而且时段是前一天8时到后一天8时总共24个小时. 两个子查询: 1.根据当前时间判断统计前天8时到今 ...

  6. Python:XXX missing X required positional argument: 'self'

    代码的一个小小Bug有时候会让人焦头烂额,费了很大劲搞明白的问题,如果不记录下来,是很容易遗忘的! 定义一个类,如果按照以下的方式使用,则会出现TypeError: testFunc() missin ...

  7. Sass开发环境搭建

    一.Ruby环境下Sass的安装      a.安装Ruby           1.下载rubyinstaller安装           2.命令行或者直接使用sass gem包安装Sass.   ...

  8. LDAP与migrationtools 导入系统账号

    1:安装migrationtools yum -y install migrationtools 2:修改配置文件 cd     /usr/share/migrationtools 可以看到很多的文件 ...

  9. Linux(Centos) 安装windows字体

    有时候在Linux中需要用到windows字体,比如微软雅黑字体,这个时候,可能就需要我们手动去安装字体了(当然,如果服务器上没装过),简单几步如下: 1.在$WINDOWS/Fonts目录中找到对应 ...

  10. http之100-continue(转)

    1.http 100-continue用于客户端在发送POST数据给服务器前,征询服务器情况,看服务器是否处理POST的数据,如果不处理,客户端则不上传POST数据,如果处理,则POST上传数据.在现 ...