WinForm 文本框验证
这是一个自定义控件,继承了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 文本框验证的更多相关文章
- 记录Js 文本框验证 与 IE兼容性
最近的日常就是将测试小姐姐提交的bug进行修改,想来这种事情还是比较好开展的,毕竟此项目已上线一年多,现在只是一些前端的问题需要改正.实际上手的时候并不是这样,原项目是在谷歌上运行,后来由于要新增一个 ...
- js实现文本框验证和实现小数的加减乘除
<script type="text/javascript"> //加法 var m=accAdd(1.22,1.22); //减法 var m1=accSub(1.2 ...
- js文本框验证
1.文本框只能输入数字代码(小数点也不能输入) <input onkeyup="this.value=this.value.replace(/\D/g,'')" onafte ...
- Axure文本框验证和外部url的调用
文本框的验证和外部url的调用: 场景: 当输入文本框中的内容是满足下面条件时:输入4-10的数字,页面会跳转到QQ注册(https://ssl.zc.qq.com/v3/index-chs.html ...
- Winform文本框只能输入限定的文本
比如WInform中的文本框只能输入数字活着字母和退格键,e.kaychar(按下键盘的值)
- JS的文本框验证以及form表单的提交阻止
js: 1.只能输入数字 只能输入数字:<input type="text" onkeyup="javascript:ReNumber(this)" /& ...
- c# winform文本框数字,数值校验
文本框数字,数值校验 public void DigitCheck_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.I ...
- 22.文本框验证和外部url的调用
面板可以右键固定到浏览器并且横向纵向都剧中 如果要在图片上进行点击或者其他操作 可以覆盖一个图片热区或者矩形(透明的)充当一个按钮的操作 这个提示的图片是默认隐藏的 通过右上角那个隐藏的勾 文本框右边 ...
- C# Winform 文本框默认提示信息
private string Notes = "提示文本"; private void textBox1_Leave(object sender, EventArgs e) { / ...
随机推荐
- const char **
foo (const char **p){ } main (int argh,char **argv) { foo(argv); } warning : argument is i ...
- SOME:收缩数据库日志文件,查看表数据量和空间占用,查看表结构索引修改时间
---收缩数据库日志文件 USE [master]ALTER DATABASE yourdatabasename SET RECOVERY SIMPLE WITH NO_WAITALTER DATAB ...
- Spark 读取HBase和SolrCloud数据
Spark1.6.2读取SolrCloud 5.5.1 //httpmime-4.4.1.jar // solr-solrj-5.5.1.jar //spark-solr-2.2.2-20161007 ...
- [Docker] docker 基础学习笔记5(共6篇)
docker 配置文件的位置: centos : /etc/sysconfig/docker ubuntu: /etc/default/docker 现在比如我自己电脑上已经装好了docker,但 ...
- 初学c# -- 学习笔记(三)
结合前面学的许多东西,写了一个小程序.会话.自定义滚动条.css等等.小程序没有用数据库,主要不知道该用哪种,以后再说吧.登录也简单,就输入用户名就可以了. 百度是个好东西,写程序时候,需要什么图就直 ...
- WPF自适应窗体实现小结
WPF自适应窗体实现小结 这几天,因工作需要,要对一个小软件进行UI调整.主要内容就是让其能够实现自适应窗体(包括文字和图标),做成像WIN7下的Media Center一样的UI.自适应窗体,顾名思 ...
- MyEclipse/Eclipse新建项目并且部署到服务器(tomcat)下
1新建项目:file/new/web project/项目名/ok 2部署到tomcat下:tomcat右单机>>add/remove/选择项目/添加/完成(tomcat下有新建的项目) ...
- sql server 2008 外键关联的设置和取消
直接上图片 选中表右击-设计 找到需要设置外键的字段.右击-关系,在弹出的对话框中点击添加 选择右边的小按钮点击.选择主键表和关联的主键ID,以及外建表的关联字段. 建立外键完成. 删除的话选中某个外 ...
- const用法小结
const与指针 char *const p --> char *(const p) --> 指针常量 char const *p --> char (const *p) --> ...
- Mob.com 短信验证的简单使用
1.环境配置 http://wiki.sharesdk.cn/android-短信sdk集成文档/ a.sdk下载 http://www.mob.com/#/downloadDetail/SMS/an ...