VerificationComponent.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class VerificationComponent.
  29. /// Implements the <see cref="System.ComponentModel.Component" />
  30. /// Implements the <see cref="System.ComponentModel.IExtenderProvider" />
  31. /// </summary>
  32. /// <seealso cref="System.ComponentModel.Component" />
  33. /// <seealso cref="System.ComponentModel.IExtenderProvider" />
  34. [ProvideProperty("VerificationModel", typeof(Control))]
  35. [ProvideProperty("VerificationCustomRegex", typeof(Control))]
  36. [ProvideProperty("VerificationRequired", typeof(Control))]
  37. [ProvideProperty("VerificationErrorMsg", typeof(Control))]
  38. [DefaultEvent("Verificationed")]
  39. public class VerificationComponent : Component, IExtenderProvider
  40. {
  41. /// <summary>
  42. /// Delegate VerificationedHandle
  43. /// </summary>
  44. /// <param name="e">The <see cref="VerificationEventArgs"/> instance containing the event data.</param>
  45. public delegate void VerificationedHandle(VerificationEventArgs e);
  46. /// <summary>
  47. /// Occurs when [verificationed].
  48. /// </summary>
  49. [Browsable(true), Category("自定义属性"), Description("验证事件"), Localizable(true)]
  50. public event VerificationedHandle Verificationed;
  51. /// <summary>
  52. /// The m control cache
  53. /// </summary>
  54. Dictionary<Control, VerificationModel> m_controlCache = new Dictionary<Control, VerificationModel>();
  55. /// <summary>
  56. /// The m control regex cache
  57. /// </summary>
  58. Dictionary<Control, string> m_controlRegexCache = new Dictionary<Control, string>();
  59. /// <summary>
  60. /// The m control required cache
  61. /// </summary>
  62. Dictionary<Control, bool> m_controlRequiredCache = new Dictionary<Control, bool>();
  63. /// <summary>
  64. /// The m control MSG cache
  65. /// </summary>
  66. Dictionary<Control, string> m_controlMsgCache = new Dictionary<Control, string>();
  67. /// <summary>
  68. /// The m control tips
  69. /// </summary>
  70. Dictionary<Control, Forms.FrmAnchorTips> m_controlTips = new Dictionary<Control, Forms.FrmAnchorTips>();
  71. /// <summary>
  72. /// The error tips back color
  73. /// </summary>
  74. private Color errorTipsBackColor = Color.FromArgb(255, 77, 58);
  75. /// <summary>
  76. /// Gets or sets the color of the error tips back.
  77. /// </summary>
  78. /// <value>The color of the error tips back.</value>
  79. [Browsable(true), Category("自定义属性"), Description("错误提示背景色"), Localizable(true)]
  80. public Color ErrorTipsBackColor
  81. {
  82. get { return errorTipsBackColor; }
  83. set { errorTipsBackColor = value; }
  84. }
  85. /// <summary>
  86. /// The error tips fore color
  87. /// </summary>
  88. private Color errorTipsForeColor = Color.White;
  89. /// <summary>
  90. /// Gets or sets the color of the error tips fore.
  91. /// </summary>
  92. /// <value>The color of the error tips fore.</value>
  93. [Browsable(true), Category("自定义属性"), Description("错误提示文字颜色"), Localizable(true)]
  94. public Color ErrorTipsForeColor
  95. {
  96. get { return errorTipsForeColor; }
  97. set { errorTipsForeColor = value; }
  98. }
  99. private int autoCloseErrorTipsTime = 3000;
  100. [Browsable(true), Category("自定义属性"), Description("自动关闭提示事件,当值为0时不自动关闭"), Localizable(true)]
  101. public int AutoCloseErrorTipsTime
  102. {
  103. get { return autoCloseErrorTipsTime; }
  104. set
  105. {
  106. if (value < 0)
  107. return;
  108. autoCloseErrorTipsTime = value;
  109. }
  110. }
  111. #region 构造函数 English:Constructor
  112. /// <summary>
  113. /// Initializes a new instance of the <see cref="VerificationComponent"/> class.
  114. /// </summary>
  115. public VerificationComponent()
  116. {
  117. }
  118. /// <summary>
  119. /// Initializes a new instance of the <see cref="VerificationComponent"/> class.
  120. /// </summary>
  121. /// <param name="container">The container.</param>
  122. public VerificationComponent(IContainer container)
  123. : this()
  124. {
  125. container.Add(this);
  126. }
  127. #endregion
  128. #region 指定此对象是否可以将其扩展程序属性提供给指定的对象。 English:Specifies whether this object can provide its extender properties to the specified object.
  129. /// <summary>
  130. /// 指定此对象是否可以将其扩展程序属性提供给指定的对象。
  131. /// </summary>
  132. /// <param name="extendee">要接收扩展程序属性的 <see cref="T:System.Object" />。</param>
  133. /// <returns>如果此对象可以扩展程序属性提供给指定对象,则为 true;否则为 false。</returns>
  134. public bool CanExtend(object extendee)
  135. {
  136. if (extendee is TextBoxBase || extendee is UCTextBoxEx || extendee is ComboBox || extendee is UCCombox)
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142. #endregion
  143. #region 验证规则 English:Validation rule
  144. /// <summary>
  145. /// Gets the verification model.
  146. /// </summary>
  147. /// <param name="control">The control.</param>
  148. /// <returns>VerificationModel.</returns>
  149. [Browsable(true), Category("自定义属性"), Description("验证规则"), DisplayName("VerificationModel"), Localizable(true)]
  150. public VerificationModel GetVerificationModel(Control control)
  151. {
  152. if (m_controlCache.ContainsKey(control))
  153. {
  154. return m_controlCache[control];
  155. }
  156. else
  157. return VerificationModel.None;
  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. #region 自定义正则 English:Custom Rules
  170. /// <summary>
  171. /// Gets the verification custom regex.
  172. /// </summary>
  173. /// <param name="control">The control.</param>
  174. /// <returns>System.String.</returns>
  175. [Browsable(true), Category("自定义属性"), Description("自定义验证正则表达式"), DisplayName("VerificationCustomRegex"), Localizable(true)]
  176. public string GetVerificationCustomRegex(Control control)
  177. {
  178. if (m_controlRegexCache.ContainsKey(control))
  179. {
  180. return m_controlRegexCache[control];
  181. }
  182. else
  183. return "";
  184. }
  185. /// <summary>
  186. /// Sets the verification custom regex.
  187. /// </summary>
  188. /// <param name="control">The control.</param>
  189. /// <param name="strRegex">The string regex.</param>
  190. public void SetVerificationCustomRegex(Control control, string strRegex)
  191. {
  192. m_controlRegexCache[control] = strRegex;
  193. }
  194. #endregion
  195. #region 必填 English:Must fill
  196. /// <summary>
  197. /// Gets the verification required.
  198. /// </summary>
  199. /// <param name="control">The control.</param>
  200. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
  201. [Browsable(true), Category("自定义属性"), Description("是否必填项"), DisplayName("VerificationRequired"), Localizable(true)]
  202. public bool GetVerificationRequired(Control control)
  203. {
  204. if (m_controlRequiredCache.ContainsKey(control))
  205. return m_controlRequiredCache[control];
  206. return false;
  207. }
  208. /// <summary>
  209. /// Sets the verification required.
  210. /// </summary>
  211. /// <param name="control">The control.</param>
  212. /// <param name="blnRequired">if set to <c>true</c> [BLN required].</param>
  213. public void SetVerificationRequired(Control control, bool blnRequired)
  214. {
  215. m_controlRequiredCache[control] = blnRequired;
  216. }
  217. #endregion
  218. #region 提示信息 English:Prompt information
  219. /// <summary>
  220. /// Gets the verification error MSG.
  221. /// </summary>
  222. /// <param name="control">The control.</param>
  223. /// <returns>System.String.</returns>
  224. [Browsable(true), Category("自定义属性"), Description("验证错误提示信息,当为空时则使用默认提示信息"), DisplayName("VerificationErrorMsg"), Localizable(true)]
  225. public string GetVerificationErrorMsg(Control control)
  226. {
  227. if (m_controlMsgCache.ContainsKey(control))
  228. return m_controlMsgCache[control];
  229. return "";
  230. }
  231. /// <summary>
  232. /// Sets the verification error MSG.
  233. /// </summary>
  234. /// <param name="control">The control.</param>
  235. /// <param name="strErrorMsg">The string error MSG.</param>
  236. public void SetVerificationErrorMsg(Control control, string strErrorMsg)
  237. {
  238. m_controlMsgCache[control] = strErrorMsg;
  239. }
  240. #endregion
  241. #region 验证 English:Verification
  242. /// <summary>
  243. /// 功能描述:验证 English:Verification result processing
  244. /// 作  者:HZH
  245. /// 创建日期:2019-09-28 09:02:49
  246. /// 任务编号:POS
  247. /// </summary>
  248. /// <param name="c">c</param>
  249. /// <returns>返回值</returns>
  250. public bool Verification(Control c)
  251. {
  252. bool bln = true;
  253. if (m_controlCache.ContainsKey(c))
  254. {
  255. var vm = m_controlCache[c];
  256. string strRegex = "";
  257. string strErrMsg = "";
  258. #region 获取正则或默认错误提示 English:Get regular or error prompts
  259. if (vm == VerificationModel.Custom)
  260. {
  261. //自定义正则
  262. if (m_controlRegexCache.ContainsKey(c))
  263. {
  264. strRegex = m_controlRegexCache[c];
  265. strErrMsg = "不正确的输入";
  266. }
  267. }
  268. else
  269. {
  270. //获取默认正则和错误提示
  271. Type type = vm.GetType(); //获取类型
  272. MemberInfo[] memberInfos = type.GetMember(vm.ToString());
  273. if (memberInfos.Length > 0)
  274. {
  275. var atts = memberInfos[0].GetCustomAttributes(typeof(VerificationAttribute), false);
  276. if (atts.Length > 0)
  277. {
  278. var va = ((VerificationAttribute)atts[0]);
  279. strErrMsg = va.ErrorMsg;
  280. strRegex = va.Regex;
  281. }
  282. }
  283. }
  284. #endregion
  285. #region 取值 English:Value
  286. string strValue = "";
  287. if (c is TextBoxBase)
  288. {
  289. strValue = (c as TextBoxBase).Text;
  290. }
  291. else if (c is UCTextBoxEx)
  292. {
  293. strValue = (c as UCTextBoxEx).InputText;
  294. }
  295. else if (c is ComboBox)
  296. {
  297. var cbo = (c as ComboBox);
  298. if (cbo.DropDownStyle == ComboBoxStyle.DropDownList)
  299. {
  300. strValue = cbo.SelectedItem == null ? "" : cbo.SelectedValue.ToString();
  301. }
  302. else
  303. {
  304. strValue = cbo.Text;
  305. }
  306. }
  307. else if (c is UCCombox)
  308. {
  309. strValue = (c as UCCombox).SelectedText;
  310. }
  311. #endregion
  312. //自定义错误信息
  313. if (m_controlMsgCache.ContainsKey(c) && !string.IsNullOrEmpty(m_controlMsgCache[c]))
  314. strErrMsg = m_controlMsgCache[c];
  315. //检查必填项
  316. if (m_controlRequiredCache.ContainsKey(c) && m_controlRequiredCache[c])
  317. {
  318. if (string.IsNullOrEmpty(strValue))
  319. {
  320. VerControl(new VerificationEventArgs()
  321. {
  322. VerificationModel = vm,
  323. Regex = strRegex,
  324. ErrorMsg = "不能为空",
  325. IsVerifySuccess = false,
  326. Required = true,
  327. VerificationControl = c
  328. });
  329. bln = false;
  330. return false;
  331. }
  332. }
  333. //验证正则
  334. if (!string.IsNullOrEmpty(strValue))
  335. {
  336. if (!string.IsNullOrEmpty(strRegex))
  337. {
  338. if (!Regex.IsMatch(strValue, strRegex))
  339. {
  340. VerControl(new VerificationEventArgs()
  341. {
  342. VerificationModel = vm,
  343. Regex = strRegex,
  344. ErrorMsg = strErrMsg,
  345. IsVerifySuccess = false,
  346. Required = m_controlRequiredCache.ContainsKey(c) && m_controlRequiredCache[c],
  347. VerificationControl = c
  348. });
  349. bln = false;
  350. return false;
  351. }
  352. }
  353. }
  354. //没有问题出发一个成功信息
  355. VerControl(new VerificationEventArgs()
  356. {
  357. VerificationModel = vm,
  358. Regex = strRegex,
  359. ErrorMsg = strErrMsg,
  360. IsVerifySuccess = true,
  361. Required = m_controlRequiredCache.ContainsKey(c) && m_controlRequiredCache[c],
  362. VerificationControl = c
  363. });
  364. }
  365. return bln;
  366. }
  367. #endregion
  368. #region 验证 English:Verification
  369. /// <summary>
  370. /// 功能描述:验证 English:Verification
  371. /// 作  者:HZH
  372. /// 创建日期:2019-09-27 17:54:38
  373. /// 任务编号:POS
  374. /// </summary>
  375. /// <returns>返回值</returns>
  376. public bool Verification()
  377. {
  378. bool bln = true;
  379. foreach (var item in m_controlCache)
  380. {
  381. Control c = item.Key;
  382. if (!Verification(c))
  383. {
  384. bln = false;
  385. }
  386. }
  387. return bln;
  388. }
  389. #endregion
  390. #region 验证结果处理 English:Verification result processing
  391. /// <summary>
  392. /// 功能描述:验证结果处理 English:Verification result processing
  393. /// 作  者:HZH
  394. /// 创建日期:2019-09-27 17:54:59
  395. /// 任务编号:POS
  396. /// </summary>
  397. /// <param name="e">e</param>
  398. private void VerControl(VerificationEventArgs e)
  399. {
  400. //如果成功则移除失败提示
  401. if (e.IsVerifySuccess)
  402. {
  403. if (m_controlTips.ContainsKey(e.VerificationControl))
  404. {
  405. m_controlTips[e.VerificationControl].Close();
  406. m_controlTips.Remove(e.VerificationControl);
  407. }
  408. }
  409. //触发事件
  410. if (Verificationed != null)
  411. {
  412. Verificationed(e);
  413. if (e.IsProcessed)//如果已处理,则不再向下执行
  414. {
  415. return;
  416. }
  417. }
  418. //如果失败则显示提示
  419. if (!e.IsVerifySuccess)
  420. {
  421. if (m_controlTips.ContainsKey(e.VerificationControl))
  422. {
  423. m_controlTips[e.VerificationControl].StrMsg = e.ErrorMsg;
  424. }
  425. else
  426. {
  427. var tips = Forms.FrmAnchorTips.ShowTips(e.VerificationControl, e.ErrorMsg, background: errorTipsBackColor, foreColor: errorTipsForeColor, autoCloseTime: autoCloseErrorTipsTime, blnTopMost: false);
  428. tips.FormClosing += tips_FormClosing;
  429. m_controlTips[e.VerificationControl] = tips;
  430. }
  431. }
  432. }
  433. void tips_FormClosing(object sender, FormClosingEventArgs e)
  434. {
  435. foreach (var item in m_controlTips)
  436. {
  437. if (item.Value == sender)
  438. {
  439. m_controlTips.Remove(item.Key);
  440. break;
  441. }
  442. }
  443. }
  444. #endregion
  445. /// <summary>
  446. /// 关闭所有错误提示
  447. /// </summary>
  448. public void CloseErrorTips()
  449. {
  450. for (int i = 0; i < 1; )
  451. {
  452. try
  453. {
  454. foreach (var item in m_controlTips)
  455. {
  456. if (item.Value != null && !item.Value.IsDisposed)
  457. {
  458. item.Value.Close();
  459. }
  460. }
  461. }
  462. catch
  463. {
  464. continue;
  465. }
  466. i++;
  467. }
  468. m_controlTips.Clear();
  469. }
  470. /// <summary>
  471. /// 关闭指定验证控件的提示
  472. /// </summary>
  473. /// <param name="verificationControl">验证控件.</param>
  474. public void CloseErrorTips(Control verificationControl)
  475. {
  476. if (m_controlTips.ContainsKey(verificationControl))
  477. {
  478. if (m_controlTips[verificationControl] != null && !m_controlTips[verificationControl].IsDisposed)
  479. {
  480. m_controlTips[verificationControl].Close();
  481. }
  482. }
  483. }
  484. }
  485. }