前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492  <https://shang.qq.com/wpa/qunwpa?
idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>

目录

https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>

准备工作

这个窗体继承子基类窗体FrmWithOKCancel1,如果你对FrmWithOKCancel1还不了解,请移步 
(二十五)c#Winform自定义控件-有确定取消的窗体(一) <https://www.cnblogs.com/bfyx/p/11364184.html>
 查看

开始

添加一个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 //
FrmInputs34 // 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
<https://gitee.com/kwwwvagaa/net_winform_custom_control> 点个星 星吧

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信