这是一个自定义控件,继承了TextBox,在TextBox基础上添加了4个属性(下载):
1.ControlType 文本框需要验证的类型
2.ControlTypeText 显示的文字(只读)
3.IsNULL 填写的内容是否可空
4.IsPass 格式是否正确(在文本框失去焦点时验证,只读)

代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows.Forms; 6 using System.ComponentModel; 7 using System.Text.RegularExpressions; 8 9 namespace TextBoxEx 10 { 11 [System.Drawing.ToolboxBitmap(typeof(TextBox))] 12 public class MyTextBox:TextBox 13 { 14 public MyTextBox() : base() { } 15 //类型枚举 16 public enum RegexType 17 { 18 Custom, //正常 19 Number, //数字 整数或者小数 20 CNString, //汉字 21 Zip, //邮政编码 22 Email, //电子邮箱 23 Phone, //手机号码 24 Integer, //整数 25 NInteger, //负整数 26 Float, //浮点数 27 ENChar, //英文字符 28 NumChar, //数字和英文字母 29 NumLineChar, //数字、英文字母或下划线 30 Url, 31 QQ, 32 DCard, //身份证 33 IP, 34 DateTime, //日期时间 35 Date, //日期 36 Year, 37 Month, 38 Day, 39 Time, 40 } 41 42 #region 私有属性 43 44 /// 文本框类型 45 private RegexType _controlType; 46 //显示的名称 47 private string _controlTypeText = "默认"; 48 //是否可空 默认为可空 49 private bool _isNULL=true; 50 //验证是否通过 51 private bool _isPass=false; 52 #endregion 53 54 #region Properties 属性栏添加的属性 55 56 [DefaultValue(RegexType.Custom), Description("文本框类型")] 57 public RegexType ControlType 58 { 59 get { return _controlType; } 60 set 61 { 62 _controlType = value; 63 //对应显示的文字 64 this.ShowDescription(value); 65 //重新绘制控件 66 base.Invalidate(); 67 } 68 } 69 70 [DefaultValue("默认"), Description("控件验证描述")] 71 public string ControlTypeText 72 { 73 get { return _controlTypeText; } 74 } 75 76 [DefaultValue(typeof(bool), "True"), Description("内容是否可空")] 77 public bool IsNULL 78 { 79 get { return _isNULL; } 80 set { _isNULL = value; base.Invalidate(); } 81 } 82 [DefaultValue(typeof(bool), "False"), Description("填写的内容格式是否正确,只读")] 83 public bool IsPass 84 { 85 get { return _isPass; } 86 } 87 #endregion 88 //判断验证类型 89 private void Testing(RegexType value, string text) 90 { 91 //可空 验证字符串为空 92 if (_isNULL && string.IsNullOrEmpty(text.Trim())) 93 { 94 _isPass = true; 95 return; 96 } 97 //不能为空 验证字符串为空 98 if (!_isNULL&&string.IsNullOrEmpty(text)) 99 { 100 _isPass = false; 101 return; 102 } 103 //其他的两种情况都需要正则验证 104 switch (value) 105 { 106 case RegexType.Custom: 107 _isPass = true; 108 break; 109 case RegexType.Number: 110 _isPass = Proving(text, @"^-?[0-9]+\.{0,1}[0-9]*$"); 111 break; 112 case RegexType.CNString: 113 _isPass=Proving(text,@"^[\u4e00-\u9fa5]*$"); 114 break; 115 case RegexType.Zip: 116 _isPass = Proving(text, @"^[1-9]\d{5}$"); 117 break; 118 case RegexType.Email: 119 _isPass = Proving(text, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"); 120 break; 121 case RegexType.Phone: 122 _isPass = Proving(text, @"^1[2-8]{2}\d{8}$"); 123 break; 124 case RegexType.Integer: 125 _isPass = Proving(text, @"^-?[1-9]\d*$"); 126 break; 127 case RegexType.NInteger: 128 _isPass = Proving(text, @"^-[1-9]\d*$"); 129 break; 130 case RegexType.Float: 131 _isPass = Proving(text, @"^(-?\d+)(\.\d+)?$"); 132 break; 133 case RegexType.ENChar: 134 _isPass = Proving(text, @"^[A-Za-z]+$"); 135 break; 136 case RegexType.NumChar: 137 _isPass = Proving(text, @"^[A-Za-z0-9]+$"); 138 break; 139 case RegexType.NumLineChar: 140 _isPass = Proving(text, @"^[A-Za-z0-9_]+$"); 141 break; 142 case RegexType.Url: 143 _isPass = Proving(text, @"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"); 144 break; 145 case RegexType.QQ: 146 _isPass = Proving(text, @"^[1-9][0-9]{4,}$"); 147 break; 148 case RegexType.DCard: 149 _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)?$"); 150 break; 151 case RegexType.IP: 152 _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])$"); 153 break; 154 case RegexType.DateTime: 155 _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$"); 156 break; 157 case RegexType.Date: 158 _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-))$"); 159 break; 160 case RegexType.Year: 161 _isPass = Proving(text, @"^[1-9]\d{3}$"); 162 break; 163 case RegexType.Month: 164 _isPass = Proving(text, @"^(0?[123456789]|1[012])$"); 165 break; 166 case RegexType.Day: 167 _isPass = Proving(text, @"^(0?[1-9]|[12]\d|3[01])$"); 168 break; 169 case RegexType.Time: 170 _isPass = Proving(text, @"^(20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"); 171 break; 172 default: 173 break; 174 } 175 176 } 177 //格式是否正确 178 private bool Proving(string str, string regexStr) 179 { 180 Regex regex; 181 try 182 { 183 regex = new Regex(regexStr); 184 } 185 catch 186 { 187 return false; 188 } 189 return regex.IsMatch(str); 190 } 191 //重写了文本框的失去焦点事件 192 protected override void OnLeave(EventArgs e) 193 { 194 Testing(this.ControlType,this.Text); 195 base.OnLeave(e); 196 } 197 //验证类型的改变 对应改变显示的汉字 198 private void ShowDescription(RegexType value) 199 { 200 switch (value) 201 { 202 case RegexType.Custom: 203 this._controlTypeText = "默认"; 204 break; 205 case RegexType.Number: 206 this._controlTypeText = "数字"; 207 break; 208 case RegexType.CNString: 209 this._controlTypeText = "汉字"; 210 break; 211 case RegexType.Zip: 212 this._controlTypeText = "邮政编码"; 213 break; 214 case RegexType.Email: 215 this._controlTypeText = "电子邮件"; 216 break; 217 case RegexType.Phone: 218 this._controlTypeText = "手机号"; 219 break; 220 case RegexType.Integer: 221 this._controlTypeText = "整数"; 222 break; 223 case RegexType.NInteger: 224 this._controlTypeText = "负整数"; 225 break; 226 case RegexType.Float: 227 this._controlTypeText = "浮点数"; 228 break; 229 case RegexType.ENChar: 230 this._controlTypeText = "英文字符"; 231 break; 232 case RegexType.NumChar: 233 this._controlTypeText = "数字和英文字母"; 234 break; 235 case RegexType.NumLineChar: 236 this._controlTypeText = "数字、英文字母或下划线"; 237 break; 238 case RegexType.Url: 239 this._controlTypeText = "URL"; 240 break; 241 case RegexType.QQ: 242 this._controlTypeText = "QQ"; 243 break; 244 case RegexType.DCard: 245 this._controlTypeText = "身份证"; 246 break; 247 case RegexType.IP: 248 this._controlTypeText = "IP"; 249 break; 250 case RegexType.DateTime: 251 this._controlTypeText = "年-月-日 时:分:秒"; 252 break; 253 case RegexType.Date: 254 this._controlTypeText = "年-月-日"; 255 break; 256 case RegexType.Year: 257 this._controlTypeText = "年份"; 258 break; 259 case RegexType.Month: 260 this._controlTypeText = "月份"; 261 break; 262 case RegexType.Day: 263 this._controlTypeText = "日期"; 264 break; 265 case RegexType.Time: 266 this._controlTypeText = "时:分:秒"; 267 break; 268 default: 269 break; 270 } 271 } 272 } 273 }
最后生成一个dll,在左侧文本框中点反键-》选择项-》浏览,选中生成的dll文件就可以了(添加引用),像平常的TextBox一样的用法,从工具栏拖入界面,然后设置属性,最后判断IsPass。
如果有需要添加的格式验证,需在代码中添加:
1.RegexType类型枚举中添加类型
2.Testing方法中添加正则表达式
3.ShowDescription方法中添加选择验证类型对应显示的汉字

浙公网安备 33010602011771号