这是一个自定义控件,继承了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. Peterson算法

    #define FALSE 0 #define TRUE 1 #define N 2 /*进程数量 */ int turn; /* 现在轮到谁 */ int interested[N]; /*所有值初 ...

  2. 正则表达式提取string 中的表名

    简单版本: Regex reg = new Regex(@"(?i)\bfrom\b(?![^\[\]]*\])\s+(\[[^\[\]]+\]|\S+)"); MatchColl ...

  3. asp.net前台代码中引入namespace的方法

    <%@ import NameSpace="System.Data.OleDb" %>

  4. 在Asp.net 4.0 中动态注册HttpModule

    using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...

  5. XML数据 JSON数据 LitJSON 数据 的编写和解析 小结

    用XML生成如下数据<?xml version="1.0"encoding="UTF-8"?><Transform name="My ...

  6. AspNet MVC与T4,我定制的视图模板

    一. 遇到的问题 文章开头部分想先说一下自己的困惑,在用AspNet MVC时,完成Action的编写,然后添加一个视图,这个时候弹出一个添加视图的选项窗口,如下: 很熟悉吧,继续上面说的,我添加一个 ...

  7. eclipse maven spring mvc el表达式无效

    http://www.myexception.cn/javascript/2031310.html

  8. OpenGL ES crash notes 01 - Nice to meet you

    这篇笔记完全参照<OpenGL.ES.3.0.Programming.Guide.2nd.Edition>,摘出部分内容只为学习参考. 为什么要用英文:无论是D3D的SDK还是OES的Sp ...

  9. 【Python全栈笔记】05 [模块二] 19 Oct 文件的操作

    文件操作 一般步骤1. 文件打开 2. 文件操作 3. 文件关闭 1. 打开文件 使用open(文件名(绝对路径), 打开模式, 编码) 文件打开的模式有: r:  只读模式(默认) w: 只写模式 ...

  10. Android版本4.0~7.0

    Android 4.0 Ice Cream Sandwich(冰激凌三明治):2011年10月19日发布 版本主要更新如下:全新的UI:全新的Chrome Lite浏览器:截图功能:更强大的图片编辑功 ...