(七十六)c#Winform自定义控件-表单验证组件
程序员文章站
2023-11-17 19:58:58
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
github:https://github.com/kwwwvagaa/netwinformcontrol
码云:
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
来都来了,点个【推荐】再走吧,谢谢
nuget
install-package hzh_controls
目录
用处及效果
准备工作
思路如下:
1、确定哪些控件需要进行验证,在组件中进行属性扩展
2、定义验证规则
3、根据验证规则的正则表达式进行验证和非空验证
4、触发验证结果事件
5、进行验证结果提示
开始
添加一个验证规则枚举
1 /// <summary> 2 /// 验证规则 3 /// </summary> 4 public enum verificationmodel 5 { 6 /// <summary> 7 /// 无 8 /// </summary> 9 [description("无"), verificationattribute()] 10 none = 1, 11 /// <summary> 12 /// 任意字母数字下划线 13 /// </summary> 14 [description("任意字母数字下划线"), verificationattribute(@"^[a-za-z_0-1]*$", "请输入任意字母数字下划线")] 15 anychar = 2, 16 /// <summary> 17 /// 任意数字 18 /// </summary> 19 [description("任意数字"), verificationattribute(@"^[\-\+]?\d+(\.\d+)?$", "请输入任意数字")] 20 number = 4, 21 /// <summary> 22 /// 非负数 23 /// </summary> 24 [description("非负数"), verificationattribute(@"^(\+)?\d+(\.\d+)?$", "请输入非负数")] 25 unsignnumber = 8, 26 /// <summary> 27 /// 正数 28 /// </summary> 29 [description("正数"), verificationattribute(@"(\+)?([1-9][0-9]*(\.\d{1,2})?)|(0\.\d{1,2})", "请输入正数")] 30 positivenumber = 16, 31 /// <summary> 32 /// 整数 33 /// </summary> 34 [description("整数"), verificationattribute(@"^[\+\-]?\d+$", "请输入整数")] 35 integer = 32, 36 /// <summary> 37 /// 非负整数 38 /// </summary> 39 [description("非负整数"), verificationattribute(@"^(\+)?\d+$", "请输入非负整数")] 40 unsignintegernumber = 64, 41 /// <summary> 42 /// 正整数 43 /// </summary> 44 [description("正整数"), verificationattribute(@"^[0-9]*[1-9][0-9]*$", "请输入正整数")] 45 positiveintegernumber = 128, 46 /// <summary> 47 /// 邮箱 48 /// </summary> 49 [description("邮箱"), verificationattribute(@"^(([0-9a-za-z]+)|([0-9a-za-z]+[_.0-9a-za-z-]*[0-9a-za-z]+))@([a-za-z0-9-]+[.])+([a-za-z]{2}|net|net|com|com|gov|gov|mil|mil|org|org|edu|edu|int|int)$", "请输入正确的邮箱地址")] 50 email = 256, 51 /// <summary> 52 /// 手机 53 /// </summary> 54 [description("手机"), verificationattribute(@"^(\+?86)?1\d{10}$", "请输入正确的手机号")] 55 phone = 512, 56 /// <summary> 57 /// ip 58 /// </summary> 59 [description("ip"), verificationattribute(@"(?=(\b|\d))(((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{1,2})|(2[0-4]\d)|(25[0-5]))(?=(\b|\d))", "请输入正确的ip地址")] 60 ip = 1024, 61 /// <summary> 62 /// url 63 /// </summary> 64 [description("url"), verificationattribute(@"^[a-za-z]+://(//w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//s*)?$", "请输入正确的网址")] 65 url = 2048, 66 /// <summary> 67 /// 身份证号 68 /// </summary> 69 [description("身份证号"), verificationattribute(@"^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9xx]$", "请输入正确的身份证号")] 70 idcardno = 4096, 71 /// <summary> 72 /// 正则验证 73 /// </summary> 74 [description("自定义正则表达式"), verificationattribute()] 75 custom = 8192, 76 }
还有一个验证规则枚举的特性
1 public class verificationattribute : attribute 2 { 3 /// <summary> 4 /// initializes a new instance of the <see cref="verificationattribute"/> class. 5 /// </summary> 6 /// <param name="strregex">the string regex.</param> 7 /// <param name="strerrormsg">the string error msg.</param> 8 public verificationattribute(string strregex = "", string strerrormsg = "") 9 { 10 regex = strregex; 11 errormsg = strerrormsg; 12 } 13 /// <summary> 14 /// gets or sets the regex. 15 /// </summary> 16 /// <value>the regex.</value> 17 public string regex { get; set; } 18 /// <summary> 19 /// gets or sets the error msg. 20 /// </summary> 21 /// <value>the error msg.</value> 22 public string errormsg { get; set; } 23 24 }
定义事件参数
1 public class verificationeventargs : eventargs 2 { 3 /// <summary> 4 /// gets or sets the verification control. 5 /// </summary> 6 /// <value>the verification control.</value> 7 public control verificationcontrol { get; set; } 8 /// <summary> 9 /// gets or sets a value indicating whether [verify success]. 10 /// </summary> 11 /// <value><c>true</c> if [verify success]; otherwise, <c>false</c>.</value> 12 public bool isverifysuccess { get; set; } 13 /// <summary> 14 /// gets or sets the verification model. 15 /// </summary> 16 /// <value>the verification model.</value> 17 public verificationmodel verificationmodel { get; set; } 18 /// <summary> 19 /// 是否已处理,如果为true,则不再使用默认验证提示功能 20 /// </summary> 21 /// <value><c>true</c> if this instance is processed; otherwise, <c>false</c>.</value> 22 public bool isprocessed { get; set; } 23 /// <summary> 24 /// gets or sets 正则表达式 25 /// </summary> 26 /// <value>the custom regex.</value> 27 public string regex { get; set; } 28 /// <summary> 29 /// gets or sets a value indicating whether this <see cref="verificationeventargs"/> is required. 30 /// </summary> 31 /// <value><c>true</c> if required; otherwise, <c>false</c>.</value> 32 public bool required { get; set; } 33 34 /// <summary> 35 /// gets or sets the error msg. 36 /// </summary> 37 /// <value>the error msg.</value> 38 public string errormsg { get; set; } 39 }
添加一个类verificationcomponent继承component,实现接口 iextenderprovider以对控件进行扩展
定义属性
1 /// <summary> 2 /// delegate verificationedhandle 3 /// </summary> 4 /// <param name="e">the <see cref="verificationeventargs"/> instance containing the event data.</param> 5 public delegate void verificationedhandle(verificationeventargs e); 6 /// <summary> 7 /// occurs when [verificationed]. 8 /// </summary> 9 [browsable(true), category("自定义属性"), description("验证事件"), localizable(true)] 10 public event verificationedhandle verificationed; 11 12 /// <summary> 13 /// the m control cache 14 /// </summary> 15 dictionary<control, verificationmodel> m_controlcache = new dictionary<control, verificationmodel>(); 16 /// <summary> 17 /// the m control regex cache 18 /// </summary> 19 dictionary<control, string> m_controlregexcache = new dictionary<control, string>(); 20 /// <summary> 21 /// the m control required cache 22 /// </summary> 23 dictionary<control, bool> m_controlrequiredcache = new dictionary<control, bool>(); 24 /// <summary> 25 /// the m control msg cache 26 /// </summary> 27 dictionary<control, string> m_controlmsgcache = new dictionary<control, string>(); 28 /// <summary> 29 /// the m control tips 30 /// </summary> 31 dictionary<control, forms.frmanchortips> m_controltips = new dictionary<control, forms.frmanchortips>(); 32 33 /// <summary> 34 /// the error tips back color 35 /// </summary> 36 private color errortipsbackcolor = color.fromargb(255, 77, 58); 37 38 /// <summary> 39 /// gets or sets the color of the error tips back. 40 /// </summary> 41 /// <value>the color of the error tips back.</value> 42 [browsable(true), category("自定义属性"), description("错误提示背景色"), localizable(true)] 43 public color errortipsbackcolor 44 { 45 get { return errortipsbackcolor; } 46 set { errortipsbackcolor = value; } 47 } 48 49 /// <summary> 50 /// the error tips fore color 51 /// </summary> 52 private color errortipsforecolor = color.white; 53 54 /// <summary> 55 /// gets or sets the color of the error tips fore. 56 /// </summary> 57 /// <value>the color of the error tips fore.</value> 58 [browsable(true), category("自定义属性"), description("错误提示文字颜色"), localizable(true)] 59 public color errortipsforecolor 60 { 61 get { return errortipsforecolor; } 62 set { errortipsforecolor = value; } 63 }
哪些控件需要进行验证(属性扩展)
1 public bool canextend(object extendee) 2 { 3 if (extendee is textboxbase || extendee is uctextboxex || extendee is combobox || extendee is uccombox) 4 { 5 return true; 6 } 7 return false; 8 }
扩展属性
1 /// <summary> 2 /// the m control cache 3 /// </summary> 4 dictionary<control, verificationmodel> m_controlcache = new dictionary<control, verificationmodel>(); 5 /// <summary> 6 /// the m control regex cache 7 /// </summary> 8 dictionary<control, string> m_controlregexcache = new dictionary<control, string>(); 9 /// <summary> 10 /// the m control required cache 11 /// </summary> 12 dictionary<control, bool> m_controlrequiredcache = new dictionary<control, bool>(); 13 /// <summary> 14 /// the m control msg cache 15 /// </summary> 16 dictionary<control, string> m_controlmsgcache = new dictionary<control, string>(); 17 18 #region 验证规则 english:validation rule 19 /// <summary> 20 /// gets the verification model. 21 /// </summary> 22 /// <param name="control">the control.</param> 23 /// <returns>verificationmodel.</returns> 24 [browsable(true), category("自定义属性"), description("验证规则"), displayname("verificationmodel"), localizable(true)] 25 public verificationmodel getverificationmodel(control control) 26 { 27 if (m_controlcache.containskey(control)) 28 { 29 return m_controlcache[control]; 30 } 31 else 32 return verificationmodel.none; 33 } 34 35 /// <summary> 36 /// sets the verification model. 37 /// </summary> 38 /// <param name="control">the control.</param> 39 /// <param name="vm">the vm.</param> 40 public void setverificationmodel(control control, verificationmodel vm) 41 { 42 m_controlcache[control] = vm; 43 } 44 #endregion 45 46 #region 自定义正则 english:custom rules 47 /// <summary> 48 /// gets the verification custom regex. 49 /// </summary> 50 /// <param name="control">the control.</param> 51 /// <returns>system.string.</returns> 52 [browsable(true), category("自定义属性"), description("自定义验证正则表达式"), displayname("verificationcustomregex"), localizable(true)] 53 public string getverificationcustomregex(control control) 54 { 55 if (m_controlregexcache.containskey(control)) 56 { 57 return m_controlregexcache[control]; 58 } 59 else 60 return ""; 61 } 62 63 /// <summary> 64 /// sets the verification custom regex. 65 /// </summary> 66 /// <param name="control">the control.</param> 67 /// <param name="strregex">the string regex.</param> 68 public void setverificationcustomregex(control control, string strregex) 69 { 70 m_controlregexcache[control] = strregex; 71 } 72 #endregion 73 74 #region 必填 english:must fill 75 /// <summary> 76 /// gets the verification required. 77 /// </summary> 78 /// <param name="control">the control.</param> 79 /// <returns><c>true</c> if xxxx, <c>false</c> otherwise.</returns> 80 [browsable(true), category("自定义属性"), description("是否必填项"), displayname("verificationrequired"), localizable(true)] 81 public bool getverificationrequired(control control) 82 { 83 if (m_controlrequiredcache.containskey(control)) 84 return m_controlrequiredcache[control]; 85 return false; 86 } 87 88 /// <summary> 89 /// sets the verification required. 90 /// </summary> 91 /// <param name="control">the control.</param> 92 /// <param name="blnrequired">if set to <c>true</c> [bln required].</param> 93 public void setverificationrequired(control control, bool blnrequired) 94 { 95 m_controlrequiredcache[control] = blnrequired; 96 } 97 #endregion 98 99 #region 提示信息 english:prompt information 100 /// <summary> 101 /// gets the verification error msg. 102 /// </summary> 103 /// <param name="control">the control.</param> 104 /// <returns>system.string.</returns> 105 [browsable(true), category("自定义属性"), description("验证错误提示信息,当为空时则使用默认提示信息"), displayname("verificationerrormsg"), localizable(true)] 106 public string getverificationerrormsg(control control) 107 { 108 if (m_controlmsgcache.containskey(control)) 109 return m_controlmsgcache[control]; 110 return ""; 111 } 112 113 /// <summary> 114 /// sets the verification error msg. 115 /// </summary> 116 /// <param name="control">the control.</param> 117 /// <param name="strerrormsg">the string error msg.</param> 118 public void setverificationerrormsg(control control, string strerrormsg) 119 { 120 m_controlmsgcache[control] = strerrormsg; 121 } 122 #endregion
验证处理
1 #region 验证 english:verification 2 /// <summary> 3 /// 功能描述:验证 english:verification result processing 4 /// 作 者:hzh 5 /// 创建日期:2019-09-28 09:02:49 6 /// 任务编号:pos 7 /// </summary> 8 /// <param name="c">c</param> 9 /// <returns>返回值</returns> 10 public bool verification(control c) 11 { 12 bool bln = true; 13 if (m_controlcache.containskey(c)) 14 { 15 var vm = m_controlcache[c]; 16 string strregex = ""; 17 string strerrmsg = ""; 18 #region 获取正则或默认错误提示 english:get regular or error prompts 19 if (vm == verificationmodel.custom) 20 { 21 //自定义正则 22 if (m_controlregexcache.containskey(c)) 23 { 24 strregex = m_controlregexcache[c]; 25 strerrmsg = "不正确的输入"; 26 } 27 } 28 else 29 { 30 //获取默认正则和错误提示 31 type type = vm.gettype(); //获取类型 32 memberinfo[] memberinfos = type.getmember(vm.tostring()); 33 if (memberinfos.length > 0) 34 { 35 var atts = memberinfos[0].getcustomattributes(typeof(verificationattribute), false); 36 if (atts.length > 0) 37 { 38 var va = ((verificationattribute)atts[0]); 39 strerrmsg = va.errormsg; 40 strregex = va.regex; 41 } 42 } 43 } 44 #endregion 45 46 #region 取值 english:value 47 string strvalue = ""; 48 if (c is textboxbase) 49 { 50 strvalue = (c as textboxbase).text; 51 } 52 else if (c is uctextboxex) 53 { 54 strvalue = (c as uctextboxex).inputtext; 55 } 56 else if (c is combobox) 57 { 58 var cbo = (c as combobox); 59 if (cbo.dropdownstyle == comboboxstyle.dropdownlist) 60 { 61 strvalue = cbo.selecteditem == null ? "" : cbo.selectedvalue.tostring(); 62 } 63 else 64 { 65 strvalue = cbo.text; 66 } 67 } 68 else if (c is uccombox) 69 { 70 strvalue = (c as uccombox).selectedtext; 71 } 72 #endregion 73 74 //自定义错误信息 75 if (m_controlmsgcache.containskey(c) && !string.isnullorempty(m_controlmsgcache[c])) 76 strerrmsg = m_controlmsgcache[c]; 77 78 //检查必填项 79 if (m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c]) 80 { 81 if (string.isnullorempty(strvalue)) 82 { 83 vercontrol(new verificationeventargs() 84 { 85 verificationmodel = vm, 86 regex = strregex, 87 errormsg = "不能为空", 88 isverifysuccess = false, 89 required = true, 90 verificationcontrol = c 91 }); 92 bln = false; 93 return false; 94 } 95 } 96 //验证正则 97 if (!string.isnullorempty(strvalue)) 98 { 99 if (!string.isnullorempty(strregex)) 100 { 101 if (!regex.ismatch(strvalue, strregex)) 102 { 103 vercontrol(new verificationeventargs() 104 { 105 verificationmodel = vm, 106 regex = strregex, 107 errormsg = strerrmsg, 108 isverifysuccess = false, 109 required = m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c], 110 verificationcontrol = c 111 }); 112 bln = false; 113 return false; 114 } 115 } 116 } 117 //没有问题出发一个成功信息 118 vercontrol(new verificationeventargs() 119 { 120 verificationmodel = vm, 121 regex = strregex, 122 errormsg = strerrmsg, 123 isverifysuccess = true, 124 required = m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c], 125 verificationcontrol = c 126 }); 127 } 128 return bln; 129 } 130 #endregion 131 #region 验证 english:verification 132 /// <summary> 133 /// 功能描述:验证 english:verification 134 /// 作 者:hzh 135 /// 创建日期:2019-09-27 17:54:38 136 /// 任务编号:pos 137 /// </summary> 138 /// <returns>返回值</returns> 139 public bool verification() 140 { 141 bool bln = true; 142 foreach (var item in m_controlcache) 143 { 144 control c = item.key; 145 if (!verification(c)) 146 { 147 bln = false; 148 } 149 } 150 return bln; 151 } 152 #endregion 153 154 155 156 #region 验证结果处理 english:verification result processing 157 /// <summary> 158 /// 功能描述:验证结果处理 english:verification result processing 159 /// 作 者:hzh 160 /// 创建日期:2019-09-27 17:54:59 161 /// 任务编号:pos 162 /// </summary> 163 /// <param name="e">e</param> 164 private void vercontrol(verificationeventargs e) 165 { 166 //如果成功则移除失败提示 167 if (e.isverifysuccess) 168 { 169 if (m_controltips.containskey(e.verificationcontrol)) 170 { 171 m_controltips[e.verificationcontrol].close(); 172 m_controltips.remove(e.verificationcontrol); 173 } 174 } 175 //触发事件 176 if (verificationed != null) 177 { 178 verificationed(e); 179 if (e.isprocessed)//如果已处理,则不再向下执行 180 { 181 return; 182 } 183 } 184 //如果失败则显示提示 185 if (!e.isverifysuccess) 186 { 187 if (m_controltips.containskey(e.verificationcontrol)) 188 { 189 m_controltips[e.verificationcontrol].strmsg = e.errormsg; 190 } 191 else 192 { 193 var tips = forms.frmanchortips.showtips(e.verificationcontrol, e.errormsg, background: errortipsbackcolor, forecolor: errortipsforecolor, autoclosetime: 0, blntopmost: false); 194 m_controltips[e.verificationcontrol] = tips; 195 } 196 } 197 } 198 #endregion
完整代码
1 // *********************************************************************** 2 // assembly : hzh_controls 3 // created : 2019-09-27 4 // 5 // *********************************************************************** 6 // <copyright file="verificationcomponent.cs"> 7 // copyright by huang zhenghui(黄正辉) all, qq group:568015492 qq:623128629 email:623128629@qq.com 8 // </copyright> 9 // 10 // blog: https://www.cnblogs.com/bfyx 11 // github:https://github.com/kwwwvagaa/netwinformcontrol 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 13 // 14 // if you use this code, please keep this note. 15 // *********************************************************************** 16 using system; 17 using system.collections.generic; 18 using system.componentmodel; 19 using system.drawing; 20 using system.linq; 21 using system.reflection; 22 using system.text; 23 using system.text.regularexpressions; 24 using system.windows.forms; 25 26 namespace hzh_controls.controls 27 { 28 /// <summary> 29 /// class verificationcomponent. 30 /// implements the <see cref="system.componentmodel.component" /> 31 /// implements the <see cref="system.componentmodel.iextenderprovider" /> 32 /// </summary> 33 /// <seealso cref="system.componentmodel.component" /> 34 /// <seealso cref="system.componentmodel.iextenderprovider" /> 35 [provideproperty("verificationmodel", typeof(control))] 36 [provideproperty("verificationcustomregex", typeof(control))] 37 [provideproperty("verificationrequired", typeof(control))] 38 [provideproperty("verificationerrormsg", typeof(control))] 39 [defaultevent("verificationed")] 40 public class verificationcomponent : component, iextenderprovider 41 { 42 /// <summary> 43 /// delegate verificationedhandle 44 /// </summary> 45 /// <param name="e">the <see cref="verificationeventargs"/> instance containing the event data.</param> 46 public delegate void verificationedhandle(verificationeventargs e); 47 /// <summary> 48 /// occurs when [verificationed]. 49 /// </summary> 50 [browsable(true), category("自定义属性"), description("验证事件"), localizable(true)] 51 public event verificationedhandle verificationed; 52 53 /// <summary> 54 /// the m control cache 55 /// </summary> 56 dictionary<control, verificationmodel> m_controlcache = new dictionary<control, verificationmodel>(); 57 /// <summary> 58 /// the m control regex cache 59 /// </summary> 60 dictionary<control, string> m_controlregexcache = new dictionary<control, string>(); 61 /// <summary> 62 /// the m control required cache 63 /// </summary> 64 dictionary<control, bool> m_controlrequiredcache = new dictionary<control, bool>(); 65 /// <summary> 66 /// the m control msg cache 67 /// </summary> 68 dictionary<control, string> m_controlmsgcache = new dictionary<control, string>(); 69 /// <summary> 70 /// the m control tips 71 /// </summary> 72 dictionary<control, forms.frmanchortips> m_controltips = new dictionary<control, forms.frmanchortips>(); 73 74 /// <summary> 75 /// the error tips back color 76 /// </summary> 77 private color errortipsbackcolor = color.fromargb(255, 77, 58); 78 79 /// <summary> 80 /// gets or sets the color of the error tips back. 81 /// </summary> 82 /// <value>the color of the error tips back.</value> 83 [browsable(true), category("自定义属性"), description("错误提示背景色"), localizable(true)] 84 public color errortipsbackcolor 85 { 86 get { return errortipsbackcolor; } 87 set { errortipsbackcolor = value; } 88 } 89 90 /// <summary> 91 /// the error tips fore color 92 /// </summary> 93 private color errortipsforecolor = color.white; 94 95 /// <summary> 96 /// gets or sets the color of the error tips fore. 97 /// </summary> 98 /// <value>the color of the error tips fore.</value> 99 [browsable(true), category("自定义属性"), description("错误提示文字颜色"), localizable(true)] 100 public color errortipsforecolor 101 { 102 get { return errortipsforecolor; } 103 set { errortipsforecolor = value; } 104 } 105 106 #region 构造函数 english:constructor 107 /// <summary> 108 /// initializes a new instance of the <see cref="verificationcomponent"/> class. 109 /// </summary> 110 public verificationcomponent() 111 { 112 113 } 114 115 /// <summary> 116 /// initializes a new instance of the <see cref="verificationcomponent"/> class. 117 /// </summary> 118 /// <param name="container">the container.</param> 119 public verificationcomponent(icontainer container) 120 : this() 121 { 122 container.add(this); 123 } 124 #endregion 125 126 #region 指定此对象是否可以将其扩展程序属性提供给指定的对象。 english:specifies whether this object can provide its extender properties to the specified object. 127 /// <summary> 128 /// 指定此对象是否可以将其扩展程序属性提供给指定的对象。 129 /// </summary> 130 /// <param name="extendee">要接收扩展程序属性的 <see cref="t:system.object" />。</param> 131 /// <returns>如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。</returns> 132 public bool canextend(object extendee) 133 { 134 if (extendee is textboxbase || extendee is uctextboxex || extendee is combobox || extendee is uccombox) 135 { 136 return true; 137 } 138 return false; 139 } 140 #endregion 141 142 #region 验证规则 english:validation rule 143 /// <summary> 144 /// gets the verification model. 145 /// </summary> 146 /// <param name="control">the control.</param> 147 /// <returns>verificationmodel.</returns> 148 [browsable(true), category("自定义属性"), description("验证规则"), displayname("verificationmodel"), localizable(true)] 149 public verificationmodel getverificationmodel(control control) 150 { 151 if (m_controlcache.containskey(control)) 152 { 153 return m_controlcache[control]; 154 } 155 else 156 return verificationmodel.none; 157 } 158 159 /// <summary> 160 /// sets the verification model. 161 /// </summary> 162 /// <param name="control">the control.</param> 163 /// <param name="vm">the vm.</param> 164 public void setverificationmodel(control control, verificationmodel vm) 165 { 166 m_controlcache[control] = vm; 167 } 168 #endregion 169 170 #region 自定义正则 english:custom rules 171 /// <summary> 172 /// gets the verification custom regex. 173 /// </summary> 174 /// <param name="control">the control.</param> 175 /// <returns>system.string.</returns> 176 [browsable(true), category("自定义属性"), description("自定义验证正则表达式"), displayname("verificationcustomregex"), localizable(true)] 177 public string getverificationcustomregex(control control) 178 { 179 if (m_controlregexcache.containskey(control)) 180 { 181 return m_controlregexcache[control]; 182 } 183 else 184 return ""; 185 } 186 187 /// <summary> 188 /// sets the verification custom regex. 189 /// </summary> 190 /// <param name="control">the control.</param> 191 /// <param name="strregex">the string regex.</param> 192 public void setverificationcustomregex(control control, string strregex) 193 { 194 m_controlregexcache[control] = strregex; 195 } 196 #endregion 197 198 #region 必填 english:must fill 199 /// <summary> 200 /// gets the verification required. 201 /// </summary> 202 /// <param name="control">the control.</param> 203 /// <returns><c>true</c> if xxxx, <c>false</c> otherwise.</returns> 204 [browsable(true), category("自定义属性"), description("是否必填项"), displayname("verificationrequired"), localizable(true)] 205 public bool getverificationrequired(control control) 206 { 207 if (m_controlrequiredcache.containskey(control)) 208 return m_controlrequiredcache[control]; 209 return false; 210 } 211 212 /// <summary> 213 /// sets the verification required. 214 /// </summary> 215 /// <param name="control">the control.</param> 216 /// <param name="blnrequired">if set to <c>true</c> [bln required].</param> 217 public void setverificationrequired(control control, bool blnrequired) 218 { 219 m_controlrequiredcache[control] = blnrequired; 220 } 221 #endregion 222 223 #region 提示信息 english:prompt information 224 /// <summary> 225 /// gets the verification error msg. 226 /// </summary> 227 /// <param name="control">the control.</param> 228 /// <returns>system.string.</returns> 229 [browsable(true), category("自定义属性"), description("验证错误提示信息,当为空时则使用默认提示信息"), displayname("verificationerrormsg"), localizable(true)] 230 public string getverificationerrormsg(control control) 231 { 232 if (m_controlmsgcache.containskey(control)) 233 return m_controlmsgcache[control]; 234 return ""; 235 } 236 237 /// <summary> 238 /// sets the verification error msg. 239 /// </summary> 240 /// <param name="control">the control.</param> 241 /// <param name="strerrormsg">the string error msg.</param> 242 public void setverificationerrormsg(control control, string strerrormsg) 243 { 244 m_controlmsgcache[control] = strerrormsg; 245 } 246 #endregion 247 248 249 #region 验证 english:verification 250 /// <summary> 251 /// 功能描述:验证 english:verification result processing 252 /// 作 者:hzh 253 /// 创建日期:2019-09-28 09:02:49 254 /// 任务编号:pos 255 /// </summary> 256 /// <param name="c">c</param> 257 /// <returns>返回值</returns> 258 public bool verification(control c) 259 { 260 bool bln = true; 261 if (m_controlcache.containskey(c)) 262 { 263 var vm = m_controlcache[c]; 264 string strregex = ""; 265 string strerrmsg = ""; 266 #region 获取正则或默认错误提示 english:get regular or error prompts 267 if (vm == verificationmodel.custom) 268 { 269 //自定义正则 270 if (m_controlregexcache.containskey(c)) 271 { 272 strregex = m_controlregexcache[c]; 273 strerrmsg = "不正确的输入"; 274 } 275 } 276 else 277 { 278 //获取默认正则和错误提示 279 type type = vm.gettype(); //获取类型 280 memberinfo[] memberinfos = type.getmember(vm.tostring()); 281 if (memberinfos.length > 0) 282 { 283 var atts = memberinfos[0].getcustomattributes(typeof(verificationattribute), false); 284 if (atts.length > 0) 285 { 286 var va = ((verificationattribute)atts[0]); 287 strerrmsg = va.errormsg; 288 strregex = va.regex; 289 } 290 } 291 } 292 #endregion 293 294 #region 取值 english:value 295 string strvalue = ""; 296 if (c is textboxbase) 297 { 298 strvalue = (c as textboxbase).text; 299 } 300 else if (c is uctextboxex) 301 { 302 strvalue = (c as uctextboxex).inputtext; 303 } 304 else if (c is combobox) 305 { 306 var cbo = (c as combobox); 307 if (cbo.dropdownstyle == comboboxstyle.dropdownlist) 308 { 309 strvalue = cbo.selecteditem == null ? "" : cbo.selectedvalue.tostring(); 310 } 311 else 312 { 313 strvalue = cbo.text; 314 } 315 } 316 else if (c is uccombox) 317 { 318 strvalue = (c as uccombox).selectedtext; 319 } 320 #endregion 321 322 //自定义错误信息 323 if (m_controlmsgcache.containskey(c) && !string.isnullorempty(m_controlmsgcache[c])) 324 strerrmsg = m_controlmsgcache[c]; 325 326 //检查必填项 327 if (m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c]) 328 { 329 if (string.isnullorempty(strvalue)) 330 { 331 vercontrol(new verificationeventargs() 332 { 333 verificationmodel = vm, 334 regex = strregex, 335 errormsg = "不能为空", 336 isverifysuccess = false, 337 required = true, 338 verificationcontrol = c 339 }); 340 bln = false; 341 return false; 342 } 343 } 344 //验证正则 345 if (!string.isnullorempty(strvalue)) 346 { 347 if (!string.isnullorempty(strregex)) 348 { 349 if (!regex.ismatch(strvalue, strregex)) 350 { 351 vercontrol(new verificationeventargs() 352 { 353 verificationmodel = vm, 354 regex = strregex, 355 errormsg = strerrmsg, 356 isverifysuccess = false, 357 required = m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c], 358 verificationcontrol = c 359 }); 360 bln = false; 361 return false; 362 } 363 } 364 } 365 //没有问题出发一个成功信息 366 vercontrol(new verificationeventargs() 367 { 368 verificationmodel = vm, 369 regex = strregex, 370 errormsg = strerrmsg, 371 isverifysuccess = true, 372 required = m_controlrequiredcache.containskey(c) && m_controlrequiredcache[c], 373 verificationcontrol = c 374 }); 375 } 376 return bln; 377 } 378 #endregion 379 #region 验证 english:verification 380 /// <summary> 381 /// 功能描述:验证 english:verification 382 /// 作 者:hzh 383 /// 创建日期:2019-09-27 17:54:38 384 /// 任务编号:pos 385 /// </summary> 386 /// <returns>返回值</returns> 387 public bool verification() 388 { 389 bool bln = true; 390 foreach (var item in m_controlcache) 391 { 392 control c = item.key; 393 if (!verification(c)) 394 { 395 bln = false; 396 } 397 } 398 return bln; 399 } 400 #endregion 401 402 403 404 #region 验证结果处理 english:verification result processing 405 /// <summary> 406 /// 功能描述:验证结果处理 english:verification result processing 407 /// 作 者:hzh 408 /// 创建日期:2019-09-27 17:54:59 409 /// 任务编号:pos 410 /// </summary> 411 /// <param name="e">e</param> 412 private void vercontrol(verificationeventargs e) 413 { 414 //如果成功则移除失败提示 415 if (e.isverifysuccess) 416 { 417 if (m_controltips.containskey(e.verificationcontrol)) 418 { 419 m_controltips[e.verificationcontrol].close(); 420 m_controltips.remove(e.verificationcontrol); 421 } 422 } 423 //触发事件 424 if (verificationed != null) 425 { 426 verificationed(e); 427 if (e.isprocessed)//如果已处理,则不再向下执行 428 { 429 return; 430 } 431 } 432 //如果失败则显示提示 433 if (!e.isverifysuccess) 434 { 435 if (m_controltips.containskey(e.verificationcontrol)) 436 { 437 m_controltips[e.verificationcontrol].strmsg = e.errormsg; 438 } 439 else 440 { 441 var tips = forms.frmanchortips.showtips(e.verificationcontrol, e.errormsg, background: errortipsbackcolor, forecolor: errortipsforecolor, autoclosetime: 0, blntopmost: false); 442 m_controltips[e.verificationcontrol] = tips; 443 } 444 } 445 } 446 #endregion 447 } 448 }
最后的话
如果你喜欢的话,请到 点个星星吧
上一篇: 劈叉怎么练?坚持一个月,学不会找我
下一篇: Oracle中的半联结和反联结详解