官网

http://www.hzhcontrols.com

前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

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

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

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

准备工作

这个窗体继承子基类窗体FrmWithOKCancel1,如果你对FrmWithOKCancel1还不了解,请移步 (二十五)c#Winform自定义控件-有确定取消的窗体(一) 查看

开始

添加一个Form,命名FrmInputs,继承FrmWithOKCancel1

一个多参构造函数

  #region 构造函数
/// <summary>
/// 功能描述:构造函数
/// 作  者:HZH
/// 创建日期:2019-08-05 10:57:26
/// 任务编号:POS
/// </summary>
/// <param name="strTitle">窗体标题</param>
/// <param name="args">输入项名称</param>
/// <param name="inTypes">输入项对应输入类型,key:输入项名称,如不设置默认不控制输入</param>
/// <param name="regexs">输入项对应正则规则,当imTypes=Regex时有效,key:输入项名称,如不设置默认不控制输入</param>
/// <param name="keyBoards">文本框键盘,key:输入项名称,如不设置默认英文键盘</param>
/// <param name="mastInputs">必填输入项名称</param>
/// <param name="defaultValues">输入项默认值,key:输入项名称</param>
public FrmInputs(
string strTitle,
string[] inPutLabels,
Dictionary<string, TextInputType> inTypes = null,
Dictionary<string, string> regexs = null,
Dictionary<string, HZH_Controls.Controls.KeyBoardType> keyBoards = null,
List<string> mastInputs = null,
Dictionary<string, string> defaultValues = null)
{
InitializeComponent();
this.Title = strTitle;
if (inPutLabels.Length <= )
{
throw new Exception("输入数量不能为空");
}
try
{
Values = new string[inPutLabels.Length];
HZH_Controls.ControlHelper.FreezeControl(this, true); for (int i = inPutLabels.Length - ; i >= ; i--)
{
Panel p = new Panel();
p.Dock = DockStyle.Top;
p.Height = ;
p.Padding = new Padding(); 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(, , , );
lbl.TextAlign = ContentAlignment.MiddleRight;
lbl.AutoSize = false;
lbl.Width = ;
lbl.Dock = DockStyle.Left;
lbl.Font = new System.Drawing.Font("微软雅黑", );
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 = ;
lblT.Dock = DockStyle.Right;
lblT.Font = new System.Drawing.Font("微软雅黑", );
lblT.ForeColor = Color.Red;
p.Controls.Add(lblT);
this.panel3.Controls.Add(p);
this.ActiveControl = txt;
} this.Height = + inPutLabels.Length * ;
}
finally
{
HZH_Controls.ControlHelper.FreezeControl(this, false);
}
}
#endregion

重写DoEnter函数

 protected override void DoEnter()
{
for (int i = ; i < Values.Length; i++)
{
var cs = this.panel3.Controls.Find("txt_" + i, true);
if (cs.Length > )
{
var txt = cs[] 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();
}

完整代码如下

 // 版权所有  黄正辉  交流群: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<int, string> m_mastInputs = new Dictionary<int, string>();
#region 构造函数
/// <summary>
/// 功能描述:构造函数
/// 作  者:HZH
/// 创建日期:2019-08-05 10:57:26
/// 任务编号:POS
/// </summary>
/// <param name="strTitle">窗体标题</param>
/// <param name="args">输入项名称</param>
/// <param name="inTypes">输入项对应输入类型,key:输入项名称,如不设置默认不控制输入</param>
/// <param name="regexs">输入项对应正则规则,当imTypes=Regex时有效,key:输入项名称,如不设置默认不控制输入</param>
/// <param name="keyBoards">文本框键盘,key:输入项名称,如不设置默认英文键盘</param>
/// <param name="mastInputs">必填输入项名称</param>
/// <param name="defaultValues">输入项默认值,key:输入项名称</param>
public FrmInputs(
string strTitle,
string[] inPutLabels,
Dictionary<string, TextInputType> inTypes = null,
Dictionary<string, string> regexs = null,
Dictionary<string, HZH_Controls.Controls.KeyBoardType> keyBoards = null,
List<string> mastInputs = null,
Dictionary<string, string> defaultValues = null)
{
InitializeComponent();
this.Title = strTitle;
if (inPutLabels.Length <= )
{
throw new Exception("输入数量不能为空");
}
try
{
Values = new string[inPutLabels.Length];
HZH_Controls.ControlHelper.FreezeControl(this, true); for (int i = inPutLabels.Length - ; i >= ; i--)
{
Panel p = new Panel();
p.Dock = DockStyle.Top;
p.Height = ;
p.Padding = new Padding(); 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(, , , );
lbl.TextAlign = ContentAlignment.MiddleRight;
lbl.AutoSize = false;
lbl.Width = ;
lbl.Dock = DockStyle.Left;
lbl.Font = new System.Drawing.Font("微软雅黑", );
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 = ;
lblT.Dock = DockStyle.Right;
lblT.Font = new System.Drawing.Font("微软雅黑", );
lblT.ForeColor = Color.Red;
p.Controls.Add(lblT);
this.panel3.Controls.Add(p);
this.ActiveControl = txt;
} this.Height = + inPutLabels.Length * ;
}
finally
{
HZH_Controls.ControlHelper.FreezeControl(this, false);
}
}
#endregion protected override void DoEnter()
{
for (int i = ; i < Values.Length; i++)
{
var cs = this.panel3.Controls.Find("txt_" + i, true);
if (cs.Length > )
{
var txt = cs[] 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
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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(, );
this.Name = "FrmInputs";
this.Text = "FrmInputs";
this.ResumeLayout(false); } #endregion }
}

用处及效果

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

效果:

调用示例

   FrmInputs frm = new FrmInputs("动态多输入窗体测试",
new string[] { "姓名", "电话", "身份证号", "住址" },
new Dictionary<string, HZH_Controls.TextInputType>() { { "电话", HZH_Controls.TextInputType.Regex }, { "身份证号", HZH_Controls.TextInputType.Regex } },
new Dictionary<string, string>() { { "电话", "^1\\d{10}$" }, { "身份证号", "^\\d{18}$" } },
new Dictionary<string, KeyBoardType>() { { "电话", KeyBoardType.UCKeyBorderNum }, { "身份证号", KeyBoardType.UCKeyBorderNum } },
new List<string>() { "姓名", "电话", "身份证号" });
frm.ShowDialog(this);

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

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

  1. (二十二)c#Winform自定义控件-半透明窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. (二十四)c#Winform自定义控件-单标题窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  5. (二)c#Winform自定义控件-按钮

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  6. (四十二)c#Winform自定义控件-进度条扩展

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  7. (十七)c#Winform自定义控件-基类窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (十二)c#Winform自定义控件-分页控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

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

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. centos7 + Nginx+ HTTPS + uwsgi + python3.6 + Docker + Django1.11 + mysql 5.6 + virtualenv 环境搭建

    环境搭建: 系统: ​ centos7.2 x64 开发环境: ​ python3.6 ​ Django 1.11 虚拟环境: [Docker](https://www.runoob.com/dock ...

  2. 图像识别sift+bow+svm

    本文概述 利用SIFT特征进行简单的花朵识别 SIFT算法的特点有: SIFT特征是图像的局部特征,其对旋转.尺度缩放.亮度变化保持不变性,对视角变化.仿射变换.噪声也保持一定程度的稳定性: SIFT ...

  3. c++小游戏——杀手

    杀手小游戏 会有一个存活者:(1 2 3 4 5),如果出现(1 0 3 4 5),代表二号已经死了. 一号有3次复活权 且有一次随机诅咒权(即当自己被杀死时,会随机诅咒另外一个人,当然不是死人或自己 ...

  4. ctrl shift o失效

    是这样的.preference -> general -> keys ,找到Organize Imports ,然后 在 “WHEN”里面 要选择 Editing JAVA SOURCE. ...

  5. Python开发:NumPy学习(一)ndarray数组

    一.数据维度 一个数据表达一个含义,一组数据表达一个或多个含义. 数据维度概念:一组数据的组织形式,其中有一维数据.二维数据.多维数据.高维数据. 1.  一维数据 一维数据由对等关系的有序或无序数据 ...

  6. python面向过程编程小程序 -ATM(里面用了终端打印)

    06.09自我总结 1.文件摆放 ├── xxxx │ ├── run.py │ └── fil_mode.py │ └── data_time.py │ └── loading.py │ └── d ...

  7. hadoop之安装hadoop

    官网 http://hadoop.apache.org/ 下载页:https://hadoop.apache.org/releases.html 上传安装包到Linux 解压并进入到目录下 [root ...

  8. 一个简单的JS倒计时

    看到很多商城都是抢购倒计时的功能,今天闲来无事做了个倒计时.全当学习JS. 主要思路:主要用到Date对象,声明一个变量获取当前时间,在声明一个变量获取结束时间,结束时间-当前时间=剩余时间(倒计时) ...

  9. Java、Java SE、Java Web和Java EE的区别

    刚接触Java对这些概念上的东西有点模糊,查了很多资料,想把它分享出来,要是哪里不对请大家指正(^_^) 1.Java 毫无疑问这就是门语言和C.C++.C#一样没什么好说的. 2.Java SE和J ...

  10. 如何在 Centos7 中使用阿里云的yum源

    如何在 Centos7 中使用阿里云的yum源 1. 备份原来的yum源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Ba ...