前提 入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。 開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果覺得寫的還行,請點個 star 支持一下吧 歡迎前來交流探討: 企鵝群568015492 目錄 ...
前提
入行已經7,8年了,一直想做一套漂亮點的自定義控制項,於是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
如果覺得寫的還行,請點個 star 支持一下吧
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
準備工作
這個窗體繼承子基類窗體FrmWithOKCancel1,如果你對FrmWithOKCancel1還不瞭解,請移步 (二十五)c#Winform自定義控制項-有確定取消的窗體(一) 查看
開始
添加一個Form,命名FrmInputs,繼承FrmWithOKCancel1
一個多參構造函數
1 #region 構造函數 2 /// <summary> 3 /// 功能描述:構造函數 4 /// 作 者:HZH 5 /// 創建日期:2019-08-05 10:57:26 6 /// 任務編號:POS 7 /// </summary> 8 /// <param name="strTitle">窗體標題</param> 9 /// <param name="args">輸入項名稱</param> 10 /// <param name="inTypes">輸入項對應輸入類型,key:輸入項名稱,如不設置預設不控制輸入</param> 11 /// <param name="regexs">輸入項對應正則規則,當imTypes=Regex時有效,key:輸入項名稱,如不設置預設不控制輸入</param> 12 /// <param name="keyBoards">文本框鍵盤,key:輸入項名稱,如不設置預設英文鍵盤</param> 13 /// <param name="mastInputs">必填輸入項名稱</param> 14 /// <param name="defaultValues">輸入項預設值,key:輸入項名稱</param> 15 public FrmInputs( 16 string strTitle, 17 string[] inPutLabels, 18 Dictionary<string, TextInputType> inTypes = null, 19 Dictionary<string, string> regexs = null, 20 Dictionary<string, HZH_Controls.Controls.KeyBoardType> keyBoards = null, 21 List<string> mastInputs = null, 22 Dictionary<string, string> defaultValues = null) 23 { 24 InitializeComponent(); 25 this.Title = strTitle; 26 if (inPutLabels.Length <= 0) 27 { 28 throw new Exception("輸入數量不能為空"); 29 } 30 try 31 { 32 Values = new string[inPutLabels.Length]; 33 HZH_Controls.ControlHelper.FreezeControl(this, true); 34 35 for (int i = inPutLabels.Length - 1; i >= 0; i--) 36 { 37 Panel p = new Panel(); 38 p.Dock = DockStyle.Top; 39 p.Height = 62; 40 p.Padding = new Padding(10); 41 42 HZH_Controls.Controls.UCTextBoxEx txt = new Controls.UCTextBoxEx(); 43 txt.Dock = DockStyle.Fill; 44 txt.IsShowKeyboard = true; 45 txt.IsShowClearBtn = true; 46 txt.Name = "txt_" + i; 47 txt.TabIndex = i; 48 if (inTypes != null && inTypes.ContainsKey(inPutLabels[i])) 49 { 50 txt.InputType = inTypes[inPutLabels[i]]; 51 if (txt.InputType == TextInputType.Regex && regexs != null && regexs.ContainsKey(inPutLabels[i])) 52 txt.RegexPattern = regexs[inPutLabels[i]]; 53 } 54 if (keyBoards != null && keyBoards.ContainsKey(inPutLabels[i])) 55 txt.KeyBoardType = keyBoards[inPutLabels[i]]; 56 if (mastInputs != null && mastInputs.Contains(inPutLabels[i])) 57 { 58 m_mastInputs[i] = inPutLabels[i]; 59 } 60 if (defaultValues != null && defaultValues.ContainsKey(inPutLabels[i])) 61 txt.InputText = defaultValues[inPutLabels[i]]; 62 p.Controls.Add(txt); 63 64 Label lbl = new Label(); 65 lbl.Text = inPutLabels[i]; 66 lbl.Padding = new System.Windows.Forms.Padding(0, 0, 5, 0); 67 lbl.TextAlign = ContentAlignment.MiddleRight; 68 lbl.AutoSize = false; 69 lbl.Width = 120; 70 lbl.Dock = DockStyle.Left; 71 lbl.Font = new System.Drawing.Font("微軟雅黑", 12); 72 p.Controls.Add(lbl); 73 74 Label lblT = new Label(); 75 if (mastInputs != null && mastInputs.Contains(inPutLabels[i])) 76 { 77 lblT.Text = "*"; 78 } 79 else 80 { 81 lblT.Text = ""; 82 } 83 lblT.AutoSize = false; 84 lblT.TextAlign = ContentAlignment.MiddleLeft; 85 lblT.Width = 25; 86 lblT.Dock = DockStyle.Right; 87 lblT.Font = new System.Drawing.Font("微軟雅黑", 12); 88 lblT.ForeColor = Color.Red; 89 p.Controls.Add(lblT); 90 this.panel3.Controls.Add(p); 91 this.ActiveControl = txt; 92 } 93 94 this.Height = 124 + inPutLabels.Length * 62; 95 } 96 finally 97 { 98 HZH_Controls.ControlHelper.FreezeControl(this, false); 99 } 100 } 101 #endregion
重寫DoEnter函數
1 protected override void DoEnter() 2 { 3 for (int i = 0; i < Values.Length; i++) 4 { 5 var cs = this.panel3.Controls.Find("txt_" + i, true); 6 if (cs.Length > 0) 7 { 8 var txt = cs[0] as HZH_Controls.Controls.UCTextBoxEx; 9 Values[i] = txt.InputText; 10 if (m_mastInputs.ContainsKey(i) && string.IsNullOrWhiteSpace(txt.InputText)) 11 { 12 HZH_Controls.Forms.FrmTips.ShowTipsInfo(this, "[" + m_mastInputs[i] + "]必須輸入。"); 13 return; 14 } 15 } 16 } 17 base.DoEnter(); 18 }
完整代碼如下
1 // 版權所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:FrmInputs.cs 3 // 創建日期:2019-08-15 16:04:41 4 // 功能描述:FrmInputs 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using System.Collections.Generic; 8 using System.ComponentModel; 9 using System.Data; 10 using System.Drawing; 11 using System.Linq; 12 using System.Text; 13 using System.Windows.Forms; 14 15 namespace HZH_Controls.Forms 16 { 17 public partial class FrmInputs : FrmWithOKCancel1 18 { 19 public string[] Values { get; private set; } 20 private Dictionary<int, string> m_mastInputs = new Dictionary<int, string>(); 21 #region 構造函數 22 /// <summary> 23 /// 功能描述:構造函數 24 /// 作 者:HZH 25 /// 創建日期:2019-08-05 10:57:26 26 /// 任務編號:POS 27 /// </summary> 28 /// <param name="strTitle">窗體標題</param> 29 /// <param name="args">輸入項名稱</param> 30 /// <param name="inTypes">輸入項對應輸入類型,key:輸入項名稱,如不設置預設不控制輸入</param> 31 /// <param name="regexs">輸入項對應正則規則,當imTypes=Regex時有效,key:輸入項名稱,如不設置預設不控制輸入</param> 32 /// <param name="keyBoards">文本框鍵盤,key:輸入項名稱,如不設置預設英文鍵盤</param> 33 /// <param name="mastInputs">必填輸入項名稱</param> 34 /// <param name="defaultValues">輸入項預設值,key:輸入項名稱</param> 35 public FrmInputs( 36 string strTitle, 37 string[] inPutLabels, 38 Dictionary<string, TextInputType> inTypes = null, 39 Dictionary<string, string> regexs = null, 40 Dictionary<string, HZH_Controls.Controls.KeyBoardType> keyBoards = null, 41 List<string> mastInputs = null, 42 Dictionary<string, string> defaultValues = null) 43 { 44 InitializeComponent(); 45 this.Title = strTitle; 46 if (inPutLabels.Length <= 0) 47 { 48 throw new Exception("輸入數量不能為空"); 49 } 50 try 51 { 52 Values = new string[inPutLabels.Length]; 53 HZH_Controls.ControlHelper.FreezeControl(this, true); 54 55 for (int i = inPutLabels.Length - 1; i >= 0; i--) 56 { 57 Panel p = new Panel(); 58 p.Dock = DockStyle.Top; 59 p.Height = 62; 60 p.Padding = new Padding(10); 61 62 HZH_Controls.Controls.UCTextBoxEx txt = new Controls.UCTextBoxEx(); 63 txt.Dock = DockStyle.Fill; 64 txt.IsShowKeyboard = true; 65 txt.IsShowClearBtn = true; 66 txt.Name = "txt_" + i; 67 txt.TabIndex = i; 68 if (inTypes != null && inTypes.ContainsKey(inPutLabels[i])) 69 { 70 txt.InputType = inTypes[inPutLabels[i]]; 71 if (txt.InputType == TextInputType.Regex && regexs != null && regexs.ContainsKey(inPutLabels[i])) 72 txt.RegexPattern = regexs[inPutLabels[i]]; 73 } 74 if (keyBoards != null && keyBoards.ContainsKey(inPutLabels[i])) 75 txt.KeyBoardType = keyBoards[inPutLabels[i]]; 76 if (mastInputs != null && mastInputs.Contains(inPutLabels[i])) 77 { 78 m_mastInputs[i] = inPutLabels[i]; 79 } 80 if (defaultValues != null && defaultValues.ContainsKey(inPutLabels[i])) 81 txt.InputText = defaultValues[inPutLabels[i]]; 82 p.Controls.Add(txt); 83 84 Label lbl = new Label(); 85 lbl.Text = inPutLabels[i]; 86 lbl.Padding = new System.Windows.Forms.Padding(0, 0, 5, 0); 87 lbl.TextAlign = ContentAlignment.MiddleRight; 88 lbl.AutoSize = false; 89 lbl.Width = 120; 90 lbl.Dock = DockStyle.Left; 91 lbl.Font = new System.Drawing.Font("微軟雅黑", 12); 92 p.Controls.Add(lbl); 93 94 Label lblT = new Label(); 95 if (mastInputs != null && mastInputs.Contains(inPutLabels[i])) 96 { 97 lblT.Text = "*"; 98 } 99 else 100 { 101 lblT.Text = ""; 102 } 103 lblT.AutoSize = false; 104 lblT.TextAlign = ContentAlignment.MiddleLeft; 105 lblT.Width = 25; 106 lblT.Dock = DockStyle.Right; 107 lblT.Font = new System.Drawing.Font("微軟雅黑", 12); 108 lblT.ForeColor = Color.Red; 109 p.Controls.Add(lblT); 110 this.panel3.Controls.Add(p); 111 this.ActiveControl = txt; 112 } 113 114 this.Height = 124 + inPutLabels.Length * 62; 115 } 116 finally 117 { 118 HZH_Controls.ControlHelper.FreezeControl(this, false); 119 } 120 } 121 #endregion 122 123 protected override void DoEnter() 124 { 125 for (int i = 0; i < Values.Length; i++) 126 { 127 var cs = this.panel3.Controls.Find("txt_" + i, true); 128 if (cs.Length > 0) 129 { 130 var txt = cs[0] as HZH_Controls.Controls.UCTextBoxEx; 131 Values[i] = txt.InputText; 132 if (m_mastInputs.ContainsKey(i) && string.IsNullOrWhiteSpace(txt.InputText)) 133 { 134 HZH_Controls.Forms.FrmTips.ShowTipsInfo(this, "[" + m_mastInputs[i] + "]必須輸入。"); 135 return; 136 } 137 } 138 } 139 base.DoEnter(); 140 } 141 } 142 }View Code
1 namespace HZH_Controls.Forms 2 { 3 partial class FrmInputs 4 { 5 /// <summary> 6 /// Required designer variable. 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// Clean up any resources being used. 12 /// </summary> 13 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows Form Designer generated code 24 25 /// <summary> 26 /// Required method for Designer support - do not modify 27 /// the contents of this method with the code editor. 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.SuspendLayout(); 32 // 33 // FrmInputs 34 // 35 this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); 36 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 37 this.ClientSize = new System.Drawing.Size(427, 310); 38 this.Name = "FrmInputs"; 39 this.Text = "FrmInputs"; 40 this.ResumeLayout(false); 41 42 } 43 44 #endregion 45 46 47 } 48 }View Code
用處及效果
用處:當需要輸入多個文本時可用
效果:
調用示例
1 FrmInputs frm = new FrmInputs("動態多輸入窗體測試", 2 new string[] { "姓名", "電話", "身份證號", "住址" }, 3 new Dictionary<string, HZH_Controls.TextInputType>() { { "電話", HZH_Controls.TextInputType.Regex }, { "身份證號", HZH_Controls.TextInputType.Regex } }, 4 new Dictionary<string, string>() { { "電話", "^1\\d{10}$" }, { "身份證號", "^\\d{18}$" } }, 5 new Dictionary<string, KeyBoardType>() { { "電話", KeyBoardType.UCKeyBorderNum }, { "身份證號", KeyBoardType.UCKeyBorderNum } }, 6 new List<string>() { "姓名", "電話", "身份證號" }); 7 frm.ShowDialog(this);
最後的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星 星吧