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) { / ...
随机推荐
- 关于adb shell getprop相关
adb shell : getprop 查看机器的全部信息参数 getprop ro.serialno 查看机器的序列号 getprop ro.carrier 查看机器的CID号 getprop ro ...
- bash coding to changeNames
____通配符和正则表达式 此处的定义只针对linux 中的shell语言,对其它语言不适用 _正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配.grep.awk.sed等命令可以支持正则表 ...
- PLSQL登录数据库 报ORA -12154的诡异问题
https://q.cnblogs.com/q/89420/ 现象: 1.机器上先后安装了oracle两个版本的client.在装第一个client后,plsql可以顺利连接数据库a并登录. 2.安装 ...
- Dictionary和Hashtable的一些异同
Dictionary和Hashtable 区别: Dictionary和Hashtable 区别 Dictionary Hashtable 支持范型 不支持 需要自己做线程同步 通过调用 Synch ...
- c#自制视屏监控
项目需要开发一个监控程序,主要是监控其它电脑的操作情况. 先说下原理吧,首先我们列出做远程监控的基本步骤,远端电脑的ip,捕捉屏幕的方法,传输,接收并显示. 突然不知道怎么写下去了....... 程序 ...
- js获取及判断按键的方法
js 里面的键盘事件经常用到,所以收集了键盘事件对应的键码来分享下: keyCode 8 = BackSpace BackSpace keyCode 9 = Tab Tab keyCode 12 = ...
- HealthKit的使用
一.项目中关联HealthKit框架 1.在Capabilities选项中打开HealthyKit选项 HealthKit关联路径 首先填写好你项目的Bundle Identifier并且选好Team ...
- 获取图片工具类:BitmapUtil
package com.example.administrator.filemanager.utils;import android.content.Context;import android.gr ...
- Elasticsearch mapping
//设置mapping Put: http://192.168.1.102:9200/indexName { "settings": { , }, "mappings&q ...
- Python中的range函数用法
函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但 ...