前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_contr ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果覺得寫的還行,請點個 star 支持一下吧
來都來了,點個【推薦】再走吧,謝謝
NuGet
Install-Package HZH_Controls
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
用處及效果
準備工作
思路如下:
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>返回值