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

(三十)c#Winform自定义控件-文本框(三)

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

前提

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

开源地址:

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

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

目录

准备工作

终于到文本框了,文本框将包含原文本框扩展,透明文本框,数字输入文本框,带边框文本框

本文将讲解数字输入文本框,可以通过加减按钮来改变数字

用到了无焦点窗体和键盘,如果你还没有了解,请前往查看

(十九)c#winform自定义控件-停靠窗体

(十五)c#winform自定义控件-键盘(二)

开始

添加用户控件,命名ucnumtextbox

有这些属性

 1  [description("弹出输入键盘时发生"), category("自定义")]
 2         public event eventhandler showkeyborderevent;
 3         [description("关闭输入键盘时发生"), category("自定义")]
 4         public event eventhandler hidekeyborderevent;
 5         [description("数字改变时发生"), category("自定义")]
 6         public event eventhandler numchanged;
 7         /// <summary>
 8         /// 输入类型
 9         /// </summary>
10         [description("输入类型"), category("自定义")]
11         public textinputtype inputtype
12         {
13             get
14             {
15                 return txtnum.inputtype;
16             }
17             set
18             {
19                 if (value == textinputtype.notcontrol)
20                 {
21                     return;
22                 }
23                 txtnum.inputtype = value;
24             }
25         }
26 
27         [description("数字是否可手动编辑"), category("自定义")]
28         public bool isnumcaninput
29         {
30             get
31             {
32                 return txtnum.enabled;
33             }
34             set
35             {
36                 txtnum.enabled = value;
37             }
38         }
39         /// <summary>
40         /// 当inputtype为数字类型时,能输入的最大值
41         /// </summary>
42         [description("当inputtype为数字类型时,能输入的最大值。")]
43         public decimal maxvalue
44         {
45             get
46             {
47                 return this.txtnum.maxvalue;
48             }
49             set
50             {
51                 this.txtnum.maxvalue = value;
52             }
53         }
54         /// <summary>
55         /// 当inputtype为数字类型时,能输入的最小值
56         /// </summary>
57         [description("当inputtype为数字类型时,能输入的最小值。")]
58         public decimal minvalue
59         {
60             get
61             {
62                 return this.txtnum.minvalue;
63             }
64             set
65             {
66                 this.txtnum.minvalue = value;
67             }
68         }
69         private keyboardtype keyboardtype = keyboardtype.uckeybordernum;
70         [description("键盘样式"), category("自定义")]
71         public keyboardtype keyboardtype
72         {
73             get { return keyboardtype; }
74             set { keyboardtype = value; }
75         }
76 
77         [description("数值"), category("自定义")]
78         public decimal num
79         {
80             get { return txtnum.text.todecimal(); }
81             set { txtnum.text = value.tostring(); }
82         }
83         [description("字体"), category("自定义")]
84         public new font font
85         {
86             get
87             {
88                 return txtnum.font;
89             }
90             set
91             {
92                 txtnum.font = value;
93             }
94         }
95 
96         [description("增加按钮点击事件"), category("自定义")]
97         public event eventhandler addclick;
98         [description("减少按钮点击事件"), category("自定义")]
99         public event eventhandler minusclick;

一些事件

  1  void txtnum_textchanged(object sender, eventargs e)
  2         {
  3             if (numchanged != null)
  4             {
  5                 numchanged(txtnum.text.tostring(), e);
  6             }
  7         }
  8         forms.frmanchor m_frmanchor;
  9         private void txtnum_mousedown(object sender, mouseeventargs e)
 10         {
 11             if (isnumcaninput)
 12             {
 13                 if (keyboardtype != hzh_controls.controls.keyboardtype.null)
 14                 {
 15                     switch (keyboardtype)
 16                     {
 17                         case keyboardtype.uckeyborderall_en:
 18 
 19                             uckeyborderall keyall = new uckeyborderall();
 20                             keyall.retractclike += (a, b) => { m_frmanchor.hide(); };
 21                             keyall.enterclick += (a, b) => { m_frmanchor.hide(); };
 22                             m_frmanchor = new forms.frmanchor(this, keyall);
 23                             m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
 24 
 25                             m_frmanchor.show(this.findform());
 26                             break;
 27                         case keyboardtype.uckeybordernum:
 28 
 29                             uckeybordernum keynum = new uckeybordernum();
 30                             keynum.enterclick += (a, b) => { m_frmanchor.hide(); };
 31                             m_frmanchor = new forms.frmanchor(this, keynum);
 32                             m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
 33                             m_frmanchor.show(this.findform());
 34                             break;
 35                     }
 36                 }
 37             }
 38         }
 39 
 40         void m_frmanchor_visiblechanged(object sender, eventargs e)
 41         {
 42             if (m_frmanchor.visible)
 43             {
 44                 if (showkeyborderevent != null)
 45                 {
 46                     showkeyborderevent(this, null);
 47                 }
 48             }
 49             else
 50             {
 51                 if (hidekeyborderevent != null)
 52                 {
 53                     hidekeyborderevent(this, null);
 54                 }
 55             }
 56         }
 57 
 58         public void numaddclick()
 59         {
 60             btnadd_mousedown(null, null);
 61         }
 62 
 63         public void numminusclick()
 64         {
 65             btnminus_mousedown(null, null);
 66         }
 67 
 68         private void btnadd_mousedown(object sender, mouseeventargs e)
 69         {
 70             if (addclick != null)
 71             {
 72                 addclick(this, e);
 73             }
 74             decimal dec = this.txtnum.text.todecimal();
 75             dec++;
 76             txtnum.text = dec.tostring();
 77 
 78         }
 79 
 80         private void btnminus_mousedown(object sender, mouseeventargs e)
 81         {
 82             if (minusclick != null)
 83             {
 84                 minusclick(this, e);
 85             }
 86             decimal dec = this.txtnum.text.todecimal();
 87             dec--;
 88             txtnum.text = dec.tostring();
 89         }
 90 
 91         private void ucnumtextbox_load(object sender, eventargs e)
 92         {
 93             this.txtnum.backcolor = this.backcolor;
 94         }
 95 
 96         private void txtnum_fontchanged(object sender, eventargs e)
 97         {
 98             txtnum.location = new point(txtnum.location.x, (this.height - txtnum.height) / 2);
 99         }
100 
101         private void ucnumtextbox_backcolorchanged(object sender, eventargs e)
102         {
103             color c = this.backcolor;
104             control control = this;
105             while (c == color.transparent)
106             {
107                 control = control.parent;
108                 if (control == null)
109                     break;
110                 c = control.backcolor;
111             }
112             if (c == color.transparent)
113                 return;
114             txtnum.backcolor = c;
115         }

完整代码

  1 // 版权所有  黄正辉  交流群:568015492   qq:623128629
  2 // 文件名称:ucnumtextbox.cs
  3 // 创建日期:2019-08-15 16:03:54
  4 // 功能描述:textbox
  5 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
  6 using system;
  7 using system.collections.generic;
  8 using system.componentmodel;
  9 using system.drawing;
 10 using system.data;
 11 using system.linq;
 12 using system.text;
 13 using system.windows.forms;
 14 
 15 namespace hzh_controls.controls
 16 {
 17     [defaultevent("numchanged")]
 18     public partial class ucnumtextbox : usercontrol
 19     {
 20         [description("弹出输入键盘时发生"), category("自定义")]
 21         public event eventhandler showkeyborderevent;
 22         [description("关闭输入键盘时发生"), category("自定义")]
 23         public event eventhandler hidekeyborderevent;
 24         [description("数字改变时发生"), category("自定义")]
 25         public event eventhandler numchanged;
 26         /// <summary>
 27         /// 输入类型
 28         /// </summary>
 29         [description("输入类型"), category("自定义")]
 30         public textinputtype inputtype
 31         {
 32             get
 33             {
 34                 return txtnum.inputtype;
 35             }
 36             set
 37             {
 38                 if (value == textinputtype.notcontrol)
 39                 {
 40                     return;
 41                 }
 42                 txtnum.inputtype = value;
 43             }
 44         }
 45 
 46         [description("数字是否可手动编辑"), category("自定义")]
 47         public bool isnumcaninput
 48         {
 49             get
 50             {
 51                 return txtnum.enabled;
 52             }
 53             set
 54             {
 55                 txtnum.enabled = value;
 56             }
 57         }
 58         /// <summary>
 59         /// 当inputtype为数字类型时,能输入的最大值
 60         /// </summary>
 61         [description("当inputtype为数字类型时,能输入的最大值。")]
 62         public decimal maxvalue
 63         {
 64             get
 65             {
 66                 return this.txtnum.maxvalue;
 67             }
 68             set
 69             {
 70                 this.txtnum.maxvalue = value;
 71             }
 72         }
 73         /// <summary>
 74         /// 当inputtype为数字类型时,能输入的最小值
 75         /// </summary>
 76         [description("当inputtype为数字类型时,能输入的最小值。")]
 77         public decimal minvalue
 78         {
 79             get
 80             {
 81                 return this.txtnum.minvalue;
 82             }
 83             set
 84             {
 85                 this.txtnum.minvalue = value;
 86             }
 87         }
 88         private keyboardtype keyboardtype = keyboardtype.uckeybordernum;
 89         [description("键盘样式"), category("自定义")]
 90         public keyboardtype keyboardtype
 91         {
 92             get { return keyboardtype; }
 93             set { keyboardtype = value; }
 94         }
 95 
 96         [description("数值"), category("自定义")]
 97         public decimal num
 98         {
 99             get { return txtnum.text.todecimal(); }
100             set { txtnum.text = value.tostring(); }
101         }
102         [description("字体"), category("自定义")]
103         public new font font
104         {
105             get
106             {
107                 return txtnum.font;
108             }
109             set
110             {
111                 txtnum.font = value;
112             }
113         }
114 
115         [description("增加按钮点击事件"), category("自定义")]
116         public event eventhandler addclick;
117         [description("减少按钮点击事件"), category("自定义")]
118         public event eventhandler minusclick;
119         public ucnumtextbox()
120         {
121             initializecomponent();
122             txtnum.textchanged += txtnum_textchanged;
123         }
124 
125         void txtnum_textchanged(object sender, eventargs e)
126         {
127             if (numchanged != null)
128             {
129                 numchanged(txtnum.text.tostring(), e);
130             }
131         }
132         forms.frmanchor m_frmanchor;
133         private void txtnum_mousedown(object sender, mouseeventargs e)
134         {
135             if (isnumcaninput)
136             {
137                 if (keyboardtype != hzh_controls.controls.keyboardtype.null)
138                 {
139                     switch (keyboardtype)
140                     {
141                         case keyboardtype.uckeyborderall_en:
142 
143                             uckeyborderall keyall = new uckeyborderall();
144                             keyall.retractclike += (a, b) => { m_frmanchor.hide(); };
145                             keyall.enterclick += (a, b) => { m_frmanchor.hide(); };
146                             m_frmanchor = new forms.frmanchor(this, keyall);
147                             m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
148 
149                             m_frmanchor.show(this.findform());
150                             break;
151                         case keyboardtype.uckeybordernum:
152 
153                             uckeybordernum keynum = new uckeybordernum();
154                             keynum.enterclick += (a, b) => { m_frmanchor.hide(); };
155                             m_frmanchor = new forms.frmanchor(this, keynum);
156                             m_frmanchor.visiblechanged += m_frmanchor_visiblechanged;
157                             m_frmanchor.show(this.findform());
158                             break;
159                     }
160                 }
161             }
162         }
163 
164         void m_frmanchor_visiblechanged(object sender, eventargs e)
165         {
166             if (m_frmanchor.visible)
167             {
168                 if (showkeyborderevent != null)
169                 {
170                     showkeyborderevent(this, null);
171                 }
172             }
173             else
174             {
175                 if (hidekeyborderevent != null)
176                 {
177                     hidekeyborderevent(this, null);
178                 }
179             }
180         }
181 
182         public void numaddclick()
183         {
184             btnadd_mousedown(null, null);
185         }
186 
187         public void numminusclick()
188         {
189             btnminus_mousedown(null, null);
190         }
191 
192         private void btnadd_mousedown(object sender, mouseeventargs e)
193         {
194             if (addclick != null)
195             {
196                 addclick(this, e);
197             }
198             decimal dec = this.txtnum.text.todecimal();
199             dec++;
200             txtnum.text = dec.tostring();
201 
202         }
203 
204         private void btnminus_mousedown(object sender, mouseeventargs e)
205         {
206             if (minusclick != null)
207             {
208                 minusclick(this, e);
209             }
210             decimal dec = this.txtnum.text.todecimal();
211             dec--;
212             txtnum.text = dec.tostring();
213         }
214 
215         private void ucnumtextbox_load(object sender, eventargs e)
216         {
217             this.txtnum.backcolor = this.backcolor;
218         }
219 
220         private void txtnum_fontchanged(object sender, eventargs e)
221         {
222             txtnum.location = new point(txtnum.location.x, (this.height - txtnum.height) / 2);
223         }
224 
225         private void ucnumtextbox_backcolorchanged(object sender, eventargs e)
226         {
227             color c = this.backcolor;
228             control control = this;
229             while (c == color.transparent)
230             {
231                 control = control.parent;
232                 if (control == null)
233                     break;
234                 c = control.backcolor;
235             }
236             if (c == color.transparent)
237                 return;
238             txtnum.backcolor = c;
239         }
240     }
241 }
  1 namespace hzh_controls.controls
  2 {
  3     partial class ucnumtextbox
  4     {
  5         /// <summary> 
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private system.componentmodel.icontainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 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 组件设计器生成的代码
 24 
 25         /// <summary> 
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void initializecomponent()
 30         {
 31             this.txtnum = new hzh_controls.controls.textboxex();
 32             this.btnminus = new system.windows.forms.panel();
 33             this.btnadd = new system.windows.forms.panel();
 34             this.suspendlayout();
 35             // 
 36             // txtnum
 37             // 
 38             this.txtnum.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.left | system.windows.forms.anchorstyles.right)));
 39             this.txtnum.backcolor = system.drawing.color.fromargb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
 40             this.txtnum.borderstyle = system.windows.forms.borderstyle.none;
 41             this.txtnum.declength = 3;
 42             this.txtnum.font = new system.drawing.font("arial unicode ms", 15f);
 43             this.txtnum.inputtype = textinputtype.number;
 44             this.txtnum.location = new system.drawing.point(37, 11);
 45             this.txtnum.margin = new system.windows.forms.padding(0);
 46             this.txtnum.maxvalue = new decimal(new int[] {
 47             1000000,
 48             0,
 49             0,
 50             0});
 51             this.txtnum.minvalue = new decimal(new int[] {
 52             0,
 53             0,
 54             0,
 55             0});
 56             this.txtnum.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
 57             this.txtnum.name = "txtnum";
 58             this.txtnum.oldtext = null;
 59             this.txtnum.promptcolor = system.drawing.color.gray;
 60             this.txtnum.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
 61             this.txtnum.prompttext = "";
 62             this.txtnum.regexpattern = "";
 63             this.txtnum.size = new system.drawing.size(78, 27);
 64             this.txtnum.tabindex = 9;
 65             this.txtnum.text = "1";
 66             this.txtnum.textalign = system.windows.forms.horizontalalignment.center;
 67             this.txtnum.fontchanged += new system.eventhandler(this.txtnum_fontchanged);
 68             this.txtnum.mousedown += new system.windows.forms.mouseeventhandler(this.txtnum_mousedown);
 69             // 
 70             // btnminus
 71             // 
 72             this.btnminus.backcolor = system.drawing.color.transparent;
 73             this.btnminus.backgroundimage = global::hzh_controls.properties.resources.ic_remove_black_18dp;
 74             this.btnminus.backgroundimagelayout = system.windows.forms.imagelayout.center;
 75             this.btnminus.dock = system.windows.forms.dockstyle.left;
 76             this.btnminus.location = new system.drawing.point(2, 2);
 77             this.btnminus.name = "btnminus";
 78             this.btnminus.size = new system.drawing.size(32, 40);
 79             this.btnminus.tabindex = 6;
 80             this.btnminus.mousedown += new system.windows.forms.mouseeventhandler(this.btnminus_mousedown);
 81             // 
 82             // btnadd
 83             // 
 84             this.btnadd.backcolor = system.drawing.color.transparent;
 85             this.btnadd.backgroundimage = global::hzh_controls.properties.resources.ic_add_black_18dp;
 86             this.btnadd.backgroundimagelayout = system.windows.forms.imagelayout.center;
 87             this.btnadd.dock = system.windows.forms.dockstyle.right;
 88             this.btnadd.location = new system.drawing.point(118, 2);
 89             this.btnadd.name = "btnadd";
 90             this.btnadd.size = new system.drawing.size(32, 40);
 91             this.btnadd.tabindex = 5;
 92             this.btnadd.mousedown += new system.windows.forms.mouseeventhandler(this.btnadd_mousedown);
 93             // 
 94             // ucnumtextbox
 95             // 
 96             this.autoscalemode = system.windows.forms.autoscalemode.none;
 97             this.controls.add(this.txtnum);
 98             this.controls.add(this.btnminus);
 99             this.controls.add(this.btnadd);
100             this.name = "ucnumtextbox";
101             this.padding = new system.windows.forms.padding(2);
102             this.size = new system.drawing.size(152, 44);
103             this.load += new system.eventhandler(this.ucnumtextbox_load);
104             this.backcolorchanged += new system.eventhandler(this.ucnumtextbox_backcolorchanged);
105             this.resumelayout(false);
106             this.performlayout();
107 
108         }
109 
110         #endregion
111 
112         private system.windows.forms.panel btnadd;
113         private system.windows.forms.panel btnminus;
114         private textboxex txtnum;
115     }
116 }

设计效果

(三十)c#Winform自定义控件-文本框(三)

 

用处及效果

(三十)c#Winform自定义控件-文本框(三)

 

最后的话

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