(二十七)c#Winform自定义控件-多输入窗体

前提

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

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

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

欢迎前来交流探讨: 企鹅群568015492 

准备工作

这个窗体继承子基类窗体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 }

 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 }

用处及效果

用处:当需要输入多个文本时可用

效果:

调用示例

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://www.cnblogs.com/bfyx/p/11364255.html

时间: 2024-10-10 13:51:33

(二十七)c#Winform自定义控件-多输入窗体的相关文章

(五十二)c#Winform自定义控件-LED数字

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  麻烦博客下方点个[推荐],谢谢 NuGet Install-Package HZH_Con

(六十二)c#Winform自定义控件-警灯(工业)

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  麻烦博客下方点个[推荐],谢谢 NuGet Install-Package HZH_Con

(七十二)c#Winform自定义控件-雷达图

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  麻烦博客下方点个[推荐],谢谢 NuGet Install-Package HZH_Con

c#Winform自定义控件-目录

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  ~~~持续增加中~~~~~ 自定义控件 (一)c#Winform自定义控件-基类控件 (二

二十七、Java图形化界面设计——容器(JFrame)

摘自http://blog.csdn.net/liujun13579/article/details/7756729 二十七.Java图形化界面设计--容器(JFrame) 程序是为了方便用户使用的,因此实现图形化界面的程序编写是所有编程语言发展的必然趋势,在命令提示符下运行的程序可以让我们了解java程序的基本知识体系结构,现在就进入java图形化界面编程. 一.Java基本类(JFC) Java基本类("JavaFoundationClasses",JFC),由一些软件包组成.这些

winform 自定义控件:半透明Loading控件

winform  自定义控件:半透明Loading控件 by wgscd date:2015-05-05 效果: using System;using System.Drawing;using System.Windows.Forms;using System.ComponentModel;using System.Threading; namespace wgscd{ /// <summary> /// 自定义控件:半透明控件 /// </summary> [ToolboxBit

微软云计算介绍与实践(实践之二十七)

创建服务目录组 我们小张创建一个服务目录组,将用于基于角色的特定服务访问请求.比如小张希望人力资源组用户只显示基于AD的可用资源.特别说明:在服务管理器中的用户角色权限是累积的.下面是他的操作步骤: 1.从系统中心服务管理控制台,依次单击库 2.然后选择服务目录 3.在右侧栏中,单击创建目录组 4.从创建组向导点击下一步 5.接着我们小张创建一个目录组名为Contoso的服务目录组,并增加所有服务内容 6.从附加的成员界面点击添加 7.选择创建私有云和基础设施服务,然后单击确定 8.确认创建私有

C#中winform控制textbox输入只能为数字的方法

本文实例讲述了C#中winform控制textbox输入只能为数字的方法.分享给大家供大家参考.具体实现方法如下: 添加keyPress事件,控制键盘输入只能是自然数: 代码如下: /// <summary> /// 控制键盘输入只能是自然数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param>

(七十九)c#Winform自定义控件-导航菜单

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  来都来了,点个[推荐]再走吧,谢谢 NuGet Install-Package HZH_C