入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492
https://blog.csdn.net/kwwwvagaa/article/details/100586547
这个窗体继承子基类窗体FrmWithOKCancel1,如果你对FrmWithOKCancel1还不了解,请移步 (二十五)c#Winform自定义控件-有确定取消的窗体(一) 查看
添加一个Form,命名FrmInputs,继承FrmWithOKCancel1
一个多参构造函数
1 #region 构造函数
2 ///
3 /// 功能描述:构造函数
4 /// 作 者:HZH
5 /// 创建日期:2019-08-05 10:57:26
6 /// 任务编号:POS
7 ///
8 /// 窗体标题
9 /// 输入项名称
10 /// 输入项对应输入类型,key:输入项名称,如不设置默认不控制输入
11 /// 输入项对应正则规则,当imTypes=Regex时有效,key:输入项名称,如不设置默认不控制输入
12 /// 文本框键盘,key:输入项名称,如不设置默认英文键盘
13 /// 必填输入项名称
14 /// 输入项默认值,key:输入项名称
15 public FrmInputs(
16 string strTitle,
17 string[] inPutLabels,
18 Dictionary inTypes = null,
19 Dictionary regexs = null,
20 Dictionary keyBoards = null,
21 List mastInputs = null,
22 Dictionary 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 }
完整代码如下
// 版权所有 黄正辉 交流群:568015492 QQ:623128629
// 文件名称:FrmInputs.cs
// 创建日期:2019-08-15 16:04:41
// 功能描述:FrmInputs
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HZH_Controls.Forms
{
public partial class FrmInputs : FrmWithOKCancel1
{
public string[] Values { get; private set; }
private Dictionary m_mastInputs = new Dictionary();
#region 构造函数
///
/// 功能描述:构造函数
/// 作 者:HZH
/// 创建日期:2019-08-05 10:57:26
/// 任务编号:POS
///
/// 窗体标题
/// 输入项名称
/// 输入项对应输入类型,key:输入项名称,如不设置默认不控制输入
/// 输入项对应正则规则,当imTypes=Regex时有效,key:输入项名称,如不设置默认不控制输入
/// 文本框键盘,key:输入项名称,如不设置默认英文键盘
/// 必填输入项名称
/// 输入项默认值,key:输入项名称
public FrmInputs(
string strTitle,
string[] inPutLabels,
Dictionary inTypes = null,
Dictionary regexs = null,
Dictionary keyBoards = null,
List mastInputs = null,
Dictionary defaultValues = null)
{
InitializeComponent();
this.Title = strTitle;
if (inPutLabels.Length <= 0)
{
throw new Exception("输入数量不能为空");
}
try
{
Values = new string[inPutLabels.Length];
HZH_Controls.ControlHelper.FreezeControl(this, true);
for (int i = inPutLabels.Length - 1; i >= 0; i--)
{
Panel p = new Panel();
p.Dock = DockStyle.Top;
p.Height = 62;
p.Padding = new Padding(10);
HZH_Controls.Controls.UCTextBoxEx txt = new Controls.UCTextBoxEx();
txt.Dock = DockStyle.Fill;
txt.IsShowKeyboard = true;
txt.IsShowClearBtn = true;
txt.Name = "txt_" + i;
txt.TabIndex = i;
if (inTypes != null && inTypes.ContainsKey(inPutLabels[i]))
{
txt.InputType = inTypes[inPutLabels[i]];
if (txt.InputType == TextInputType.Regex && regexs != null && regexs.ContainsKey(inPutLabels[i]))
txt.RegexPattern = regexs[inPutLabels[i]];
}
if (keyBoards != null && keyBoards.ContainsKey(inPutLabels[i]))
txt.KeyBoardType = keyBoards[inPutLabels[i]];
if (mastInputs != null && mastInputs.Contains(inPutLabels[i]))
{
m_mastInputs[i] = inPutLabels[i];
}
if (defaultValues != null && defaultValues.ContainsKey(inPutLabels[i]))
txt.InputText = defaultValues[inPutLabels[i]];
p.Controls.Add(txt);
Label lbl = new Label();
lbl.Text = inPutLabels[i];
lbl.Padding = new System.Windows.Forms.Padding(0, 0, 5, 0);
lbl.TextAlign = ContentAlignment.MiddleRight;
lbl.AutoSize = false;
lbl.Width = 120;
lbl.Dock = DockStyle.Left;
lbl.Font = new System.Drawing.Font("微软雅黑", 12);
p.Controls.Add(lbl);
Label lblT = new Label();
if (mastInputs != null && mastInputs.Contains(inPutLabels[i]))
{
lblT.Text = "*";
}
else
{
lblT.Text = "";
}
lblT.AutoSize = false;
lblT.TextAlign = ContentAlignment.MiddleLeft;
lblT.Width = 25;
lblT.Dock = DockStyle.Right;
lblT.Font = new System.Drawing.Font("微软雅黑", 12);
lblT.ForeColor = Color.Red;
p.Controls.Add(lblT);
this.panel3.Controls.Add(p);
this.ActiveControl = txt;
}
this.Height = 124 + inPutLabels.Length * 62;
}
finally
{
HZH_Controls.ControlHelper.FreezeControl(this, false);
}
}
#endregion
protected override void DoEnter()
{
for (int i = 0; i < Values.Length; i++)
{
var cs = this.panel3.Controls.Find("txt_" + i, true);
if (cs.Length > 0)
{
var txt = cs[0] as HZH_Controls.Controls.UCTextBoxEx;
Values[i] = txt.InputText;
if (m_mastInputs.ContainsKey(i) && string.IsNullOrWhiteSpace(txt.InputText))
{
HZH_Controls.Forms.FrmTips.ShowTipsInfo(this, "[" + m_mastInputs[i] + "]必须输入。");
return;
}
}
}
base.DoEnter();
}
}
}
namespace HZH_Controls.Forms
{
partial class FrmInputs
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.SuspendLayout();
//
// FrmInputs
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(427, 310);
this.Name = "FrmInputs";
this.Text = "FrmInputs";
this.ResumeLayout(false);
}
#endregion
}
}
用处:当需要输入多个文本时可用
效果:
调用示例
1 FrmInputs frm = new FrmInputs("动态多输入窗体测试",
2 new string[] { "姓名", "电话", "身份证号", "住址" },
3 new Dictionary() { { "电话", HZH_Controls.TextInputType.Regex }, { "身份证号", HZH_Controls.TextInputType.Regex } },
4 new Dictionary() { { "电话", "^1\\d{10}$" }, { "身份证号", "^\\d{18}$" } },
5 new Dictionary() { { "电话", KeyBoardType.UCKeyBorderNum }, { "身份证号", KeyBoardType.UCKeyBorderNum } },
6 new List() { "姓名", "电话", "身份证号" });
7 frm.ShowDialog(this);
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧