欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

(二十五)c#Winform自定义控件-有确定取消的窗体(一)

程序员文章站 2023-04-04 11:23:58
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492 目录 ......

前提

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

开源地址:

如果觉得写的还行,请点个 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 }

用处及效果

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

效果:

(二十五)c#Winform自定义控件-有确定取消的窗体(一)

调用示例

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);

最后的话

如果你喜欢的话,请到  点个星 星吧