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

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

程序员文章站 2023-11-24 18:51:22
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

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

麻烦博客下方点个【推荐】,谢谢

nuget

install-package hzh_controls

目录

用处及效果

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

准备工作

使用gid+画的,不了解的话请自行百度

开始

添加一个类uclednum ,继承usercontrol

将数字拆分为单独的字,然后根据显示位置进行画出来,将显示位置定义为如下所示

    /* 显示位置序号
     *  ****1***
     *  *      *  
     *  6      2
     *  *      *
     *  ****7***
     *  *      *
     *  5      3
     *  *      *
     *  ****4***
     */

从上面可以看出,定义了1-7的位置,然后定义一个数字对应的显示列表

 1  private static dictionary<char, int[]> m_nums = new dictionary<char, int[]>();
 2         static uclednum()
 3         {
 4             m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
 5             m_nums['1'] = new int[] { 2, 3 };
 6             m_nums['2'] = new int[] { 1, 2, 5, 4, 7 };
 7             m_nums['3'] = new int[] { 1, 2, 7, 3, 4 };
 8             m_nums['4'] = new int[] { 2, 3, 6, 7 };
 9             m_nums['5'] = new int[] { 1, 6, 7, 3, 4 };
10             m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
11             m_nums['7'] = new int[] { 1, 2, 3 };
12             m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
13             m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
14             m_nums['-'] = new int[] { 7 };
15             m_nums[':'] = new int[0];
16             m_nums['.'] = new int[0];
17         }

你看到了还有“-”,“:”,“.”这3个符号,是为了时间和数字时候使用

然后定义一个矩形区域来用作绘画区域,并且在sizechanged事件中赋值

rectangle m_drawrect = rectangle.empty;
        void lednum_sizechanged(object sender, eventargs e)
        {
            m_drawrect = new rectangle(1, 1, this.width - 2, this.height - 2);
        }

然后就是几个属性

 1   private char m_value = '0';
 2 
 3         [description("值"), category("自定义")]
 4         public char value
 5         {
 6             get { return m_value; }
 7             set
 8             {
 9                 if (!m_nums.containskey(value))
10                 {
11                     return;
12                 }
13                 if (m_value != value)
14                 {
15                     m_value = value;
16                     refresh();
17                 }
18             }
19         }
20 
21         private int m_linewidth = 8;
22 
23         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
24         public int linewidth
25         {
26             get { return m_linewidth; }
27             set
28             {
29                 m_linewidth = value;
30                 refresh();
31             }
32         }
33 
34         [description("颜色"), category("自定义")]
35         public override system.drawing.color forecolor
36         {
37             get
38             {
39                 return base.forecolor;
40             }
41             set
42             {
43                 base.forecolor = value;
44             }
45         }

最重要的重绘

  1 protected override void onpaint(painteventargs e)
  2         {
  3             base.onpaint(e);
  4             e.graphics.setgdihigh();
  5             if (m_value == '.')
  6             {
  7                 rectangle r2 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.bottom - m_linewidth * 2, m_linewidth, m_linewidth);
  8                 e.graphics.fillrectangle(new solidbrush(forecolor), r2);
  9             }
 10             else if (m_value == ':')
 11             {
 12                 rectangle r1 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.top + (m_drawrect.height / 2 - m_linewidth) / 2, m_linewidth, m_linewidth);
 13                 e.graphics.fillrectangle(new solidbrush(forecolor), r1);
 14                 rectangle r2 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.top + (m_drawrect.height / 2 - m_linewidth) / 2 + m_drawrect.height / 2, m_linewidth, m_linewidth);
 15                 e.graphics.fillrectangle(new solidbrush(forecolor), r2);
 16             }
 17             else
 18             {
 19                 int[] vs = m_nums[m_value];
 20                 if (vs.contains(1))
 21                 {
 22                     graphicspath path = new graphicspath();
 23                     path.addlines(new point[] 
 24                 {
 25                     new point(m_drawrect.left + 2, m_drawrect.top),
 26                     new point(m_drawrect.right - 2, m_drawrect.top),
 27                     new point(m_drawrect.right - m_linewidth-2, m_drawrect.top+m_linewidth),
 28                     new point(m_drawrect.left + m_linewidth+2, m_drawrect.top+m_linewidth),
 29                     new point(m_drawrect.left + 2, m_drawrect.top)
 30                 });
 31                     path.closeallfigures();
 32                     e.graphics.fillpath(new solidbrush(forecolor), path);
 33                 }
 34 
 35                 if (vs.contains(2))
 36                 {
 37                     graphicspath path = new graphicspath();
 38                     path.addlines(new point[] 
 39                 {
 40                     new point(m_drawrect.right, m_drawrect.top),
 41                     new point(m_drawrect.right, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
 42                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2+m_linewidth/2),
 43                     new point(m_drawrect.right-m_linewidth, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
 44                     new point(m_drawrect.right-m_linewidth, m_drawrect.top+m_linewidth),
 45                     new point(m_drawrect.right, m_drawrect.top)
 46                 });
 47                     path.closeallfigures();
 48                     e.graphics.fillpath(new solidbrush(forecolor), path);
 49                 }
 50 
 51                 if (vs.contains(3))
 52                 {
 53                     graphicspath path = new graphicspath();
 54                     path.addlines(new point[] 
 55                 {
 56                     new point(m_drawrect.right, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
 57                     new point(m_drawrect.right, m_drawrect.bottom),
 58                     new point(m_drawrect.right-m_linewidth, m_drawrect.bottom-m_linewidth),
 59                     new point(m_drawrect.right-m_linewidth, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
 60                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2-m_linewidth/2),
 61                     new point(m_drawrect.right, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),                 
 62                 });
 63                     path.closeallfigures();
 64                     e.graphics.fillpath(new solidbrush(forecolor), path);
 65                 }
 66 
 67                 if (vs.contains(4))
 68                 {
 69                     graphicspath path = new graphicspath();
 70                     path.addlines(new point[] 
 71                 {
 72                     new point(m_drawrect.left + 2, m_drawrect.bottom),
 73                     new point(m_drawrect.right - 2, m_drawrect.bottom),
 74                     new point(m_drawrect.right - m_linewidth-2, m_drawrect.bottom-m_linewidth),
 75                     new point(m_drawrect.left + m_linewidth+2, m_drawrect.bottom-m_linewidth),
 76                     new point(m_drawrect.left + 2, m_drawrect.bottom)
 77                 });
 78                     path.closeallfigures();
 79                     e.graphics.fillpath(new solidbrush(forecolor), path);
 80                 }
 81 
 82                 if (vs.contains(5))
 83                 {
 84                     graphicspath path = new graphicspath();
 85                     path.addlines(new point[] 
 86                 {
 87                     new point(m_drawrect.left, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
 88                     new point(m_drawrect.left, m_drawrect.bottom),
 89                     new point(m_drawrect.left+m_linewidth, m_drawrect.bottom-m_linewidth),
 90                     new point(m_drawrect.left+m_linewidth, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
 91                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2-m_linewidth/2),
 92                     new point(m_drawrect.left, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),                 
 93                 });
 94                     path.closeallfigures();
 95                     e.graphics.fillpath(new solidbrush(forecolor), path);
 96                 }
 97 
 98 
 99                 if (vs.contains(6))
100                 {
101                     graphicspath path = new graphicspath();
102                     path.addlines(new point[] 
103                 {
104                     new point(m_drawrect.left, m_drawrect.top),
105                     new point(m_drawrect.left, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
106                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2+m_linewidth/2),
107                     new point(m_drawrect.left+m_linewidth, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
108                     new point(m_drawrect.left+m_linewidth, m_drawrect.top+m_linewidth),
109                     new point(m_drawrect.left, m_drawrect.top)
110                 });
111                     path.closeallfigures();
112                     e.graphics.fillpath(new solidbrush(forecolor), path);
113                 }
114 
115                 if (vs.contains(7))
116                 {
117                     graphicspath path = new graphicspath();
118                     path.addlines(new point[] 
119                 {
120                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.height/2+1),            
121                     new point(m_drawrect.left+m_linewidth, m_drawrect.height/2-m_linewidth/2+1),    
122                     new point(m_drawrect.right-m_linewidth, m_drawrect.height/2-m_linewidth/2+1),
123                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.height/2+1),
124                     new point(m_drawrect.right-m_linewidth, m_drawrect.height/2+m_linewidth/2+1),
125                     new point(m_drawrect.left+m_linewidth, m_drawrect.height/2+m_linewidth/2+1),    
126                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.height/2+1)
127                 });
128                     path.closeallfigures();
129                     e.graphics.fillpath(new solidbrush(forecolor), path);
130                 }
131             }
132         }

完工,看下完整代码和效果

  1 using system;
  2 using system.collections.generic;
  3 using system.linq;
  4 using system.text;
  5 using system.windows.forms;
  6 using system.drawing;
  7 using system.drawing.drawing2d;
  8 using system.componentmodel;
  9 
 10 namespace hzh_controls.controls
 11 {
 12     /* 显示位置序号
 13      *  ****1***
 14      *  *      *  
 15      *  6      2
 16      *  *      *
 17      *  ****7***
 18      *  *      *
 19      *  5      3
 20      *  *      *
 21      *  ****4***
 22      */
 23     public class uclednum : usercontrol
 24     {
 25         rectangle m_drawrect = rectangle.empty;
 26 
 27         private static dictionary<char, int[]> m_nums = new dictionary<char, int[]>();
 28         static uclednum()
 29         {
 30             m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
 31             m_nums['1'] = new int[] { 2, 3 };
 32             m_nums['2'] = new int[] { 1, 2, 5, 4, 7 };
 33             m_nums['3'] = new int[] { 1, 2, 7, 3, 4 };
 34             m_nums['4'] = new int[] { 2, 3, 6, 7 };
 35             m_nums['5'] = new int[] { 1, 6, 7, 3, 4 };
 36             m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
 37             m_nums['7'] = new int[] { 1, 2, 3 };
 38             m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
 39             m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
 40             m_nums['-'] = new int[] { 7 };
 41             m_nums[':'] = new int[0];
 42             m_nums['.'] = new int[0];
 43         }
 44 
 45 
 46         public uclednum()
 47         {
 48             sizechanged += lednum_sizechanged;
 49             this.autoscalemode = system.windows.forms.autoscalemode.none;
 50             size = new system.drawing.size(40, 70);
 51             if (m_drawrect == rectangle.empty)
 52                 m_drawrect = new rectangle(1, 1, this.width - 2, this.height - 2);
 53         }
 54 
 55         void lednum_sizechanged(object sender, eventargs e)
 56         {
 57             m_drawrect = new rectangle(1, 1, this.width - 2, this.height - 2);
 58         }
 59 
 60         private char m_value = '0';
 61 
 62         [description("值"), category("自定义")]
 63         public char value
 64         {
 65             get { return m_value; }
 66             set
 67             {
 68                 if (!m_nums.containskey(value))
 69                 {
 70                     return;
 71                 }
 72                 if (m_value != value)
 73                 {
 74                     m_value = value;
 75                     refresh();
 76                 }
 77             }
 78         }
 79 
 80         private int m_linewidth = 8;
 81 
 82         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
 83         public int linewidth
 84         {
 85             get { return m_linewidth; }
 86             set
 87             {
 88                 m_linewidth = value;
 89                 refresh();
 90             }
 91         }
 92 
 93         [description("颜色"), category("自定义")]
 94         public override system.drawing.color forecolor
 95         {
 96             get
 97             {
 98                 return base.forecolor;
 99             }
100             set
101             {
102                 base.forecolor = value;
103             }
104         }
105 
106         protected override void onpaint(painteventargs e)
107         {
108             base.onpaint(e);
109             e.graphics.setgdihigh();
110             if (m_value == '.')
111             {
112                 rectangle r2 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.bottom - m_linewidth * 2, m_linewidth, m_linewidth);
113                 e.graphics.fillrectangle(new solidbrush(forecolor), r2);
114             }
115             else if (m_value == ':')
116             {
117                 rectangle r1 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.top + (m_drawrect.height / 2 - m_linewidth) / 2, m_linewidth, m_linewidth);
118                 e.graphics.fillrectangle(new solidbrush(forecolor), r1);
119                 rectangle r2 = new rectangle(m_drawrect.left + (m_drawrect.width - m_linewidth) / 2, m_drawrect.top + (m_drawrect.height / 2 - m_linewidth) / 2 + m_drawrect.height / 2, m_linewidth, m_linewidth);
120                 e.graphics.fillrectangle(new solidbrush(forecolor), r2);
121             }
122             else
123             {
124                 int[] vs = m_nums[m_value];
125                 if (vs.contains(1))
126                 {
127                     graphicspath path = new graphicspath();
128                     path.addlines(new point[] 
129                 {
130                     new point(m_drawrect.left + 2, m_drawrect.top),
131                     new point(m_drawrect.right - 2, m_drawrect.top),
132                     new point(m_drawrect.right - m_linewidth-2, m_drawrect.top+m_linewidth),
133                     new point(m_drawrect.left + m_linewidth+2, m_drawrect.top+m_linewidth),
134                     new point(m_drawrect.left + 2, m_drawrect.top)
135                 });
136                     path.closeallfigures();
137                     e.graphics.fillpath(new solidbrush(forecolor), path);
138                 }
139 
140                 if (vs.contains(2))
141                 {
142                     graphicspath path = new graphicspath();
143                     path.addlines(new point[] 
144                 {
145                     new point(m_drawrect.right, m_drawrect.top),
146                     new point(m_drawrect.right, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
147                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2+m_linewidth/2),
148                     new point(m_drawrect.right-m_linewidth, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
149                     new point(m_drawrect.right-m_linewidth, m_drawrect.top+m_linewidth),
150                     new point(m_drawrect.right, m_drawrect.top)
151                 });
152                     path.closeallfigures();
153                     e.graphics.fillpath(new solidbrush(forecolor), path);
154                 }
155 
156                 if (vs.contains(3))
157                 {
158                     graphicspath path = new graphicspath();
159                     path.addlines(new point[] 
160                 {
161                     new point(m_drawrect.right, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
162                     new point(m_drawrect.right, m_drawrect.bottom),
163                     new point(m_drawrect.right-m_linewidth, m_drawrect.bottom-m_linewidth),
164                     new point(m_drawrect.right-m_linewidth, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
165                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2-m_linewidth/2),
166                     new point(m_drawrect.right, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),                 
167                 });
168                     path.closeallfigures();
169                     e.graphics.fillpath(new solidbrush(forecolor), path);
170                 }
171 
172                 if (vs.contains(4))
173                 {
174                     graphicspath path = new graphicspath();
175                     path.addlines(new point[] 
176                 {
177                     new point(m_drawrect.left + 2, m_drawrect.bottom),
178                     new point(m_drawrect.right - 2, m_drawrect.bottom),
179                     new point(m_drawrect.right - m_linewidth-2, m_drawrect.bottom-m_linewidth),
180                     new point(m_drawrect.left + m_linewidth+2, m_drawrect.bottom-m_linewidth),
181                     new point(m_drawrect.left + 2, m_drawrect.bottom)
182                 });
183                     path.closeallfigures();
184                     e.graphics.fillpath(new solidbrush(forecolor), path);
185                 }
186 
187                 if (vs.contains(5))
188                 {
189                     graphicspath path = new graphicspath();
190                     path.addlines(new point[] 
191                 {
192                     new point(m_drawrect.left, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
193                     new point(m_drawrect.left, m_drawrect.bottom),
194                     new point(m_drawrect.left+m_linewidth, m_drawrect.bottom-m_linewidth),
195                     new point(m_drawrect.left+m_linewidth, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),
196                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2-m_linewidth/2),
197                     new point(m_drawrect.left, m_drawrect.bottom-(m_drawrect.height-m_linewidth-4)/2),                 
198                 });
199                     path.closeallfigures();
200                     e.graphics.fillpath(new solidbrush(forecolor), path);
201                 }
202 
203 
204                 if (vs.contains(6))
205                 {
206                     graphicspath path = new graphicspath();
207                     path.addlines(new point[] 
208                 {
209                     new point(m_drawrect.left, m_drawrect.top),
210                     new point(m_drawrect.left, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
211                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2+m_linewidth/2),
212                     new point(m_drawrect.left+m_linewidth, m_drawrect.top+(m_drawrect.height-m_linewidth-4)/2),
213                     new point(m_drawrect.left+m_linewidth, m_drawrect.top+m_linewidth),
214                     new point(m_drawrect.left, m_drawrect.top)
215                 });
216                     path.closeallfigures();
217                     e.graphics.fillpath(new solidbrush(forecolor), path);
218                 }
219 
220                 if (vs.contains(7))
221                 {
222                     graphicspath path = new graphicspath();
223                     path.addlines(new point[] 
224                 {
225                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.height/2+1),            
226                     new point(m_drawrect.left+m_linewidth, m_drawrect.height/2-m_linewidth/2+1),    
227                     new point(m_drawrect.right-m_linewidth, m_drawrect.height/2-m_linewidth/2+1),
228                     new point(m_drawrect.right-m_linewidth/2, m_drawrect.height/2+1),
229                     new point(m_drawrect.right-m_linewidth, m_drawrect.height/2+m_linewidth/2+1),
230                     new point(m_drawrect.left+m_linewidth, m_drawrect.height/2+m_linewidth/2+1),    
231                     new point(m_drawrect.left+m_linewidth/2, m_drawrect.height/2+1)
232                 });
233                     path.closeallfigures();
234                     e.graphics.fillpath(new solidbrush(forecolor), path);
235                 }
236             }
237         }
238     }
239 }

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

 

 

 以上就是单个字符的了

 

=======================分割线==========================

下面对数字控件处理

添加一个用户控件uclednums

添加一点属性

 1 private string m_value;
 2 
 3         [description("值"), category("自定义")]
 4         public string value
 5         {
 6             get { return m_value; }
 7             set
 8             {
 9                 m_value = value;
10                 reloadvalue();
11             }
12         }
13 
14         private int m_linewidth = 8;
15 
16         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
17         public int linewidth
18         {
19             get { return m_linewidth; }
20             set
21             {
22                 m_linewidth = value;
23                 foreach (uclednum c in this.controls)
24                 {
25                     c.linewidth = value;
26                 }
27             }
28         }
29 
30         [description("颜色"), category("自定义")]
31         public override system.drawing.color forecolor
32         {
33             get
34             {
35                 return base.forecolor;
36             }
37             set
38             {
39                 base.forecolor = value;
40                 foreach (uclednum c in this.controls)
41                 {
42                     c.forecolor = value;
43                 }
44             }
45         }
46 
47         public override righttoleft righttoleft
48         {
49             get
50             {
51                 return base.righttoleft;
52             }
53             set
54             {
55                 base.righttoleft = value;
56                 reloadvalue();
57             }
58         }

加载控件的函数

 1  private void reloadvalue()
 2         {
 3             try
 4             {
 5                 controlhelper.freezecontrol(this, true);
 6                 this.controls.clear();
 7                 foreach (var item in m_value)
 8                 {
 9                     uclednum uc = new uclednum();
10                     if (righttoleft == system.windows.forms.righttoleft.yes)
11                         uc.dock = dockstyle.right;
12                     else
13                         uc.dock = dockstyle.left;
14                     uc.value = item;
15                     uc.forecolor = forecolor;
16                     uc.linewidth = m_linewidth;
17                     this.controls.add(uc);
18                     if (righttoleft == system.windows.forms.righttoleft.yes)
19                         uc.sendtoback();
20                     else
21                         uc.bringtofront();
22                 }
23             }
24             finally
25             {
26                 controlhelper.freezecontrol(this, false);
27             }
28         }

完整代码及效果

  1 using system;
  2 using system.collections.generic;
  3 using system.componentmodel;
  4 using system.drawing;
  5 using system.data;
  6 using system.linq;
  7 using system.text;
  8 using system.windows.forms;
  9 
 10 namespace hzh_controls.controls.led
 11 {
 12     public partial class uclednums : usercontrol
 13     {
 14         private string m_value;
 15 
 16         [description("值"), category("自定义")]
 17         public string value
 18         {
 19             get { return m_value; }
 20             set
 21             {
 22                 m_value = value;
 23                 reloadvalue();
 24             }
 25         }
 26 
 27         private int m_linewidth = 8;
 28 
 29         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
 30         public int linewidth
 31         {
 32             get { return m_linewidth; }
 33             set
 34             {
 35                 m_linewidth = value;
 36                 foreach (uclednum c in this.controls)
 37                 {
 38                     c.linewidth = value;
 39                 }
 40             }
 41         }
 42 
 43         [description("颜色"), category("自定义")]
 44         public override system.drawing.color forecolor
 45         {
 46             get
 47             {
 48                 return base.forecolor;
 49             }
 50             set
 51             {
 52                 base.forecolor = value;
 53                 foreach (uclednum c in this.controls)
 54                 {
 55                     c.forecolor = value;
 56                 }
 57             }
 58         }
 59 
 60         public override righttoleft righttoleft
 61         {
 62             get
 63             {
 64                 return base.righttoleft;
 65             }
 66             set
 67             {
 68                 base.righttoleft = value;
 69                 reloadvalue();
 70             }
 71         }
 72 
 73         private void reloadvalue()
 74         {
 75             try
 76             {
 77                 controlhelper.freezecontrol(this, true);
 78                 this.controls.clear();
 79                 foreach (var item in m_value)
 80                 {
 81                     uclednum uc = new uclednum();
 82                     if (righttoleft == system.windows.forms.righttoleft.yes)
 83                         uc.dock = dockstyle.right;
 84                     else
 85                         uc.dock = dockstyle.left;
 86                     uc.value = item;
 87                     uc.forecolor = forecolor;
 88                     uc.linewidth = m_linewidth;
 89                     this.controls.add(uc);
 90                     if (righttoleft == system.windows.forms.righttoleft.yes)
 91                         uc.sendtoback();
 92                     else
 93                         uc.bringtofront();
 94                 }
 95             }
 96             finally
 97             {
 98                 controlhelper.freezecontrol(this, false);
 99             }
100         }
101         public uclednums()
102         {
103             initializecomponent();
104             value = "0.00";
105         }
106     }
107 }
 1 namespace hzh_controls.controls.led
 2 {
 3     partial class uclednums
 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.suspendlayout();
32             // 
33             // uclednums
34             // 
35             this.autoscaledimensions = new system.drawing.sizef(6f, 12f);
36             this.autoscalemode = system.windows.forms.autoscalemode.font;
37             this.name = "uclednums";
38             this.size = new system.drawing.size(150, 58);
39             this.resumelayout(false);
40 
41         }
42 
43         #endregion
44     }
45 }

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

 

 

 =======================分割线==========================

下面是日期类控件了,这里偷懒,分成3个控件,分别是日期控件,时间控件,日期时间控件

先说日期控件,

添加一个用户控件ucleddata

添加属性

 1 private datetime m_value;
 2 
 3         [description("值"), category("自定义")]
 4         public datetime value
 5         {
 6             get { return m_value; }
 7             set
 8             {
 9                 m_value = value;
10                 string str = value.tostring("yyyy-mm-dd");
11                 for (int i = 0; i < str.length; i++)
12                 {
13                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
14                 }
15             }
16         }
17 
18         private int m_linewidth = 8;
19 
20         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
21         public int linewidth
22         {
23             get { return m_linewidth; }
24             set
25             {
26                 m_linewidth = value;
27                 foreach (uclednum c in this.tablelayoutpanel1.controls)
28                 {
29                     c.linewidth = value;
30                 }
31             }
32         }
33 
34         [description("颜色"), category("自定义")]
35         public override system.drawing.color forecolor
36         {
37             get
38             {
39                 return base.forecolor;
40             }
41             set
42             {
43                 base.forecolor = value;
44                 foreach (uclednum c in this.tablelayoutpanel1.controls)
45                 {
46                     c.forecolor = value;
47                 }
48             }
49         }

完整代码

 1 using system;
 2 using system.collections.generic;
 3 using system.componentmodel;
 4 using system.drawing;
 5 using system.data;
 6 using system.linq;
 7 using system.text;
 8 using system.windows.forms;
 9 
10 namespace hzh_controls.controls
11 {
12     public partial class ucleddata : usercontrol
13     {
14         private datetime m_value;
15 
16         [description("值"), category("自定义")]
17         public datetime value
18         {
19             get { return m_value; }
20             set
21             {
22                 m_value = value;
23                 string str = value.tostring("yyyy-mm-dd");
24                 for (int i = 0; i < str.length; i++)
25                 {
26                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
27                 }
28             }
29         }
30 
31         private int m_linewidth = 8;
32 
33         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
34         public int linewidth
35         {
36             get { return m_linewidth; }
37             set
38             {
39                 m_linewidth = value;
40                 foreach (uclednum c in this.tablelayoutpanel1.controls)
41                 {
42                     c.linewidth = value;
43                 }
44             }
45         }
46 
47         [description("颜色"), category("自定义")]
48         public override system.drawing.color forecolor
49         {
50             get
51             {
52                 return base.forecolor;
53             }
54             set
55             {
56                 base.forecolor = value;
57                 foreach (uclednum c in this.tablelayoutpanel1.controls)
58                 {
59                     c.forecolor = value;
60                 }
61             }
62         }
63         public ucleddata()
64         {
65             initializecomponent();
66             value = datetime.now;
67         }
68     }
69 }
  1 namespace hzh_controls.controls
  2 {
  3     partial class ucleddata
  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.tablelayoutpanel1 = new system.windows.forms.tablelayoutpanel();
 32             this.d1 = new hzh_controls.controls.uclednum();
 33             this.d2 = new hzh_controls.controls.uclednum();
 34             this.d3 = new hzh_controls.controls.uclednum();
 35             this.d4 = new hzh_controls.controls.uclednum();
 36             this.d5 = new hzh_controls.controls.uclednum();
 37             this.d6 = new hzh_controls.controls.uclednum();
 38             this.d7 = new hzh_controls.controls.uclednum();
 39             this.d8 = new hzh_controls.controls.uclednum();
 40             this.d9 = new hzh_controls.controls.uclednum();
 41             this.d10 = new hzh_controls.controls.uclednum();
 42             this.tablelayoutpanel1.suspendlayout();
 43             this.suspendlayout();
 44             // 
 45             // tablelayoutpanel1
 46             // 
 47             this.tablelayoutpanel1.columncount = 10;
 48             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 49             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 50             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 51             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 52             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 53             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 54             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 55             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 56             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 57             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 10f));
 58             this.tablelayoutpanel1.controls.add(this.d1, 0, 0);
 59             this.tablelayoutpanel1.controls.add(this.d2, 1, 0);
 60             this.tablelayoutpanel1.controls.add(this.d3, 2, 0);
 61             this.tablelayoutpanel1.controls.add(this.d4, 3, 0);
 62             this.tablelayoutpanel1.controls.add(this.d5, 4, 0);
 63             this.tablelayoutpanel1.controls.add(this.d6, 5, 0);
 64             this.tablelayoutpanel1.controls.add(this.d7, 6, 0);
 65             this.tablelayoutpanel1.controls.add(this.d8, 7, 0);
 66             this.tablelayoutpanel1.controls.add(this.d9, 8, 0);
 67             this.tablelayoutpanel1.controls.add(this.d10, 9, 0);
 68             this.tablelayoutpanel1.dock = system.windows.forms.dockstyle.fill;
 69             this.tablelayoutpanel1.location = new system.drawing.point(0, 0);
 70             this.tablelayoutpanel1.name = "tablelayoutpanel1";
 71             this.tablelayoutpanel1.rowcount = 1;
 72             this.tablelayoutpanel1.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 100f));
 73             this.tablelayoutpanel1.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.absolute, 20f));
 74             this.tablelayoutpanel1.size = new system.drawing.size(360, 58);
 75             this.tablelayoutpanel1.tabindex = 0;
 76             // 
 77             // d1
 78             // 
 79             this.d1.dock = system.windows.forms.dockstyle.fill;
 80             this.d1.linewidth = 8;
 81             this.d1.location = new system.drawing.point(3, 3);
 82             this.d1.name = "d1";
 83             this.d1.size = new system.drawing.size(30, 52);
 84             this.d1.tabindex = 0;
 85             this.d1.value = '2';
 86             // 
 87             // d2
 88             // 
 89             this.d2.dock = system.windows.forms.dockstyle.fill;
 90             this.d2.linewidth = 8;
 91             this.d2.location = new system.drawing.point(39, 3);
 92             this.d2.name = "d2";
 93             this.d2.size = new system.drawing.size(30, 52);
 94             this.d2.tabindex = 1;
 95             this.d2.value = '0';
 96             // 
 97             // d3
 98             // 
 99             this.d3.dock = system.windows.forms.dockstyle.fill;
100             this.d3.linewidth = 8;
101             this.d3.location = new system.drawing.point(75, 3);
102             this.d3.name = "d3";
103             this.d3.size = new system.drawing.size(30, 52);
104             this.d3.tabindex = 2;
105             this.d3.value = '1';
106             // 
107             // d4
108             // 
109             this.d4.dock = system.windows.forms.dockstyle.fill;
110             this.d4.linewidth = 8;
111             this.d4.location = new system.drawing.point(111, 3);
112             this.d4.name = "d4";
113             this.d4.size = new system.drawing.size(30, 52);
114             this.d4.tabindex = 3;
115             this.d4.value = '9';
116             // 
117             // d5
118             // 
119             this.d5.dock = system.windows.forms.dockstyle.fill;
120             this.d5.linewidth = 8;
121             this.d5.location = new system.drawing.point(147, 3);
122             this.d5.name = "d5";
123             this.d5.size = new system.drawing.size(30, 52);
124             this.d5.tabindex = 4;
125             this.d5.value = '-';
126             // 
127             // d6
128             // 
129             this.d6.dock = system.windows.forms.dockstyle.fill;
130             this.d6.linewidth = 8;
131             this.d6.location = new system.drawing.point(183, 3);
132             this.d6.name = "d6";
133             this.d6.size = new system.drawing.size(30, 52);
134             this.d6.tabindex = 5;
135             this.d6.value = '0';
136             // 
137             // d7
138             // 
139             this.d7.dock = system.windows.forms.dockstyle.fill;
140             this.d7.linewidth = 8;
141             this.d7.location = new system.drawing.point(219, 3);
142             this.d7.name = "d7";
143             this.d7.size = new system.drawing.size(30, 52);
144             this.d7.tabindex = 6;
145             this.d7.value = '8';
146             // 
147             // d8
148             // 
149             this.d8.dock = system.windows.forms.dockstyle.fill;
150             this.d8.linewidth = 8;
151             this.d8.location = new system.drawing.point(255, 3);
152             this.d8.name = "d8";
153             this.d8.size = new system.drawing.size(30, 52);
154             this.d8.tabindex = 7;
155             this.d8.value = '-';
156             // 
157             // d9
158             // 
159             this.d9.dock = system.windows.forms.dockstyle.fill;
160             this.d9.linewidth = 8;
161             this.d9.location = new system.drawing.point(291, 3);
162             this.d9.name = "d9";
163             this.d9.size = new system.drawing.size(30, 52);
164             this.d9.tabindex = 8;
165             this.d9.value = '0';
166             // 
167             // d10
168             // 
169             this.d10.dock = system.windows.forms.dockstyle.fill;
170             this.d10.linewidth = 8;
171             this.d10.location = new system.drawing.point(327, 3);
172             this.d10.name = "d10";
173             this.d10.size = new system.drawing.size(30, 52);
174             this.d10.tabindex = 9;
175             this.d10.value = '1';
176             // 
177             // ucleddata
178             // 
179             this.autoscalemode = system.windows.forms.autoscalemode.none;
180             this.controls.add(this.tablelayoutpanel1);
181             this.margin = new system.windows.forms.padding(0);
182             this.name = "ucleddata";
183             this.size = new system.drawing.size(360, 58);
184             this.tablelayoutpanel1.resumelayout(false);
185             this.resumelayout(false);
186 
187         }
188 
189         #endregion
190 
191         private system.windows.forms.tablelayoutpanel tablelayoutpanel1;
192         private uclednum d1;
193         private uclednum d2;
194         private uclednum d3;
195         private uclednum d4;
196         private uclednum d5;
197         private uclednum d6;
198         private uclednum d7;
199         private uclednum d8;
200         private uclednum d9;
201         private uclednum d10;
202 
203     }
204 }

 =======================分割线==========================

时间控件

添加一个用户控件ucledtime

添加属性

 1  private datetime m_value;
 2 
 3         [description("值"), category("自定义")]
 4         public datetime value
 5         {
 6             get { return m_value; }
 7             set
 8             {
 9                 m_value = value;
10                 string str = value.tostring("hh:mm:ss");
11                 for (int i = 0; i < str.length; i++)
12                 {
13                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
14                 }
15             }
16         }
17 
18         private int m_linewidth = 8;
19 
20         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
21         public int linewidth
22         {
23             get { return m_linewidth; }
24             set
25             {
26                 m_linewidth = value;
27                 foreach (uclednum c in this.tablelayoutpanel1.controls)
28                 {
29                     c.linewidth = value;
30                 }
31             }
32         }
33 
34         [description("颜色"), category("自定义")]
35         public override system.drawing.color forecolor
36         {
37             get
38             {
39                 return base.forecolor;
40             }
41             set
42             {
43                 base.forecolor = value;
44                 foreach (uclednum c in this.tablelayoutpanel1.controls)
45                 {
46                     c.forecolor = value;
47                 }
48             }
49         }

全部代码

 1 using system;
 2 using system.collections.generic;
 3 using system.componentmodel;
 4 using system.drawing;
 5 using system.data;
 6 using system.linq;
 7 using system.text;
 8 using system.windows.forms;
 9 
10 namespace hzh_controls.controls
11 {
12     public partial class ucledtime : usercontrol
13     {
14         private datetime m_value;
15 
16         [description("值"), category("自定义")]
17         public datetime value
18         {
19             get { return m_value; }
20             set
21             {
22                 m_value = value;
23                 string str = value.tostring("hh:mm:ss");
24                 for (int i = 0; i < str.length; i++)
25                 {
26                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
27                 }
28             }
29         }
30 
31         private int m_linewidth = 8;
32 
33         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
34         public int linewidth
35         {
36             get { return m_linewidth; }
37             set
38             {
39                 m_linewidth = value;
40                 foreach (uclednum c in this.tablelayoutpanel1.controls)
41                 {
42                     c.linewidth = value;
43                 }
44             }
45         }
46 
47         [description("颜色"), category("自定义")]
48         public override system.drawing.color forecolor
49         {
50             get
51             {
52                 return base.forecolor;
53             }
54             set
55             {
56                 base.forecolor = value;
57                 foreach (uclednum c in this.tablelayoutpanel1.controls)
58                 {
59                     c.forecolor = value;
60                 }
61             }
62         }
63         public ucledtime()
64         {
65             initializecomponent();
66             value = datetime.now;
67         }
68     }
69 }
  1 namespace hzh_controls.controls
  2 {
  3     partial class ucledtime
  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.tablelayoutpanel1 = new system.windows.forms.tablelayoutpanel();
 32             this.d1 = new hzh_controls.controls.uclednum();
 33             this.d2 = new hzh_controls.controls.uclednum();
 34             this.d3 = new hzh_controls.controls.uclednum();
 35             this.d4 = new hzh_controls.controls.uclednum();
 36             this.d5 = new hzh_controls.controls.uclednum();
 37             this.d6 = new hzh_controls.controls.uclednum();
 38             this.d7 = new hzh_controls.controls.uclednum();
 39             this.d8 = new hzh_controls.controls.uclednum();
 40             this.tablelayoutpanel1.suspendlayout();
 41             this.suspendlayout();
 42             // 
 43             // tablelayoutpanel1
 44             // 
 45             this.tablelayoutpanel1.columncount = 8;
 46             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 47             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 48             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 49             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 50             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 51             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 52             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 53             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 12.5f));
 54             this.tablelayoutpanel1.controls.add(this.d1, 0, 0);
 55             this.tablelayoutpanel1.controls.add(this.d2, 1, 0);
 56             this.tablelayoutpanel1.controls.add(this.d3, 2, 0);
 57             this.tablelayoutpanel1.controls.add(this.d4, 3, 0);
 58             this.tablelayoutpanel1.controls.add(this.d5, 4, 0);
 59             this.tablelayoutpanel1.controls.add(this.d6, 5, 0);
 60             this.tablelayoutpanel1.controls.add(this.d7, 6, 0);
 61             this.tablelayoutpanel1.controls.add(this.d8, 7, 0);
 62             this.tablelayoutpanel1.dock = system.windows.forms.dockstyle.fill;
 63             this.tablelayoutpanel1.location = new system.drawing.point(0, 0);
 64             this.tablelayoutpanel1.name = "tablelayoutpanel1";
 65             this.tablelayoutpanel1.rowcount = 1;
 66             this.tablelayoutpanel1.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 100f));
 67             this.tablelayoutpanel1.size = new system.drawing.size(290, 58);
 68             this.tablelayoutpanel1.tabindex = 0;
 69             // 
 70             // d1
 71             // 
 72             this.d1.dock = system.windows.forms.dockstyle.fill;
 73             this.d1.linewidth = 8;
 74             this.d1.location = new system.drawing.point(3, 3);
 75             this.d1.name = "d1";
 76             this.d1.size = new system.drawing.size(30, 52);
 77             this.d1.tabindex = 0;
 78             this.d1.value = '2';
 79             // 
 80             // d2
 81             // 
 82             this.d2.dock = system.windows.forms.dockstyle.fill;
 83             this.d2.linewidth = 8;
 84             this.d2.location = new system.drawing.point(39, 3);
 85             this.d2.name = "d2";
 86             this.d2.size = new system.drawing.size(30, 52);
 87             this.d2.tabindex = 1;
 88             this.d2.value = '3';
 89             // 
 90             // d3
 91             // 
 92             this.d3.dock = system.windows.forms.dockstyle.fill;
 93             this.d3.linewidth = 8;
 94             this.d3.location = new system.drawing.point(75, 3);
 95             this.d3.name = "d3";
 96             this.d3.size = new system.drawing.size(30, 52);
 97             this.d3.tabindex = 2;
 98             this.d3.value = ':';
 99             // 
100             // d4
101             // 
102             this.d4.dock = system.windows.forms.dockstyle.fill;
103             this.d4.linewidth = 8;
104             this.d4.location = new system.drawing.point(111, 3);
105             this.d4.name = "d4";
106             this.d4.size = new system.drawing.size(30, 52);
107             this.d4.tabindex = 3;
108             this.d4.value = '1';
109             // 
110             // d5
111             // 
112             this.d5.dock = system.windows.forms.dockstyle.fill;
113             this.d5.linewidth = 8;
114             this.d5.location = new system.drawing.point(147, 3);
115             this.d5.name = "d5";
116             this.d5.size = new system.drawing.size(30, 52);
117             this.d5.tabindex = 4;
118             this.d5.value = '0';
119             // 
120             // d6
121             // 
122             this.d6.dock = system.windows.forms.dockstyle.fill;
123             this.d6.linewidth = 8;
124             this.d6.location = new system.drawing.point(183, 3);
125             this.d6.name = "d6";
126             this.d6.size = new system.drawing.size(30, 52);
127             this.d6.tabindex = 5;
128             this.d6.value = ':';
129             // 
130             // d7
131             // 
132             this.d7.dock = system.windows.forms.dockstyle.fill;
133             this.d7.linewidth = 8;
134             this.d7.location = new system.drawing.point(219, 3);
135             this.d7.name = "d7";
136             this.d7.size = new system.drawing.size(30, 52);
137             this.d7.tabindex = 6;
138             this.d7.value = '1';
139             // 
140             // d8
141             // 
142             this.d8.dock = system.windows.forms.dockstyle.fill;
143             this.d8.linewidth = 8;
144             this.d8.location = new system.drawing.point(255, 3);
145             this.d8.name = "d8";
146             this.d8.size = new system.drawing.size(32, 52);
147             this.d8.tabindex = 7;
148             this.d8.value = '0';
149             // 
150             // ucledtime
151             // 
152             this.autoscalemode = system.windows.forms.autoscalemode.none;
153             this.controls.add(this.tablelayoutpanel1);
154             this.name = "ucledtime";
155             this.size = new system.drawing.size(290, 58);
156             this.tablelayoutpanel1.resumelayout(false);
157             this.resumelayout(false);
158 
159         }
160 
161         #endregion
162 
163         private system.windows.forms.tablelayoutpanel tablelayoutpanel1;
164         private uclednum d1;
165         private uclednum d2;
166         private uclednum d3;
167         private uclednum d4;
168         private uclednum d5;
169         private uclednum d6;
170         private uclednum d7;
171         private uclednum d8;
172     }
173 }

 =======================分割线==========================

日期时间控件

添加一个用户控件ucleddatatime

添加属性

 1  private datetime m_value;
 2 
 3         [description("值"), category("自定义")]
 4         public datetime value
 5         {
 6             get { return m_value; }
 7             set
 8             {
 9                 m_value = value;
10                 string str = value.tostring("yyyy-mm-dd hh:mm:ss");
11                 for (int i = 0; i < str.length; i++)
12                 {
13                     if (i == 10)
14                         continue;
15                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
16                 }
17             }
18         }
19 
20         private int m_linewidth = 8;
21 
22         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
23         public int linewidth
24         {
25             get { return m_linewidth; }
26             set
27             {
28                 m_linewidth = value;
29                 foreach (uclednum c in this.tablelayoutpanel1.controls)
30                 {
31                     c.linewidth = value;
32                 }
33             }
34         }
35 
36         [description("颜色"), category("自定义")]
37         public override system.drawing.color forecolor
38         {
39             get
40             {
41                 return base.forecolor;
42             }
43             set
44             {
45                 base.forecolor = value;
46                 foreach (uclednum c in this.tablelayoutpanel1.controls)
47                 {
48                     c.forecolor = value;
49                 }
50             }
51         }

全部代码

 1 using system;
 2 using system.collections.generic;
 3 using system.componentmodel;
 4 using system.drawing;
 5 using system.data;
 6 using system.linq;
 7 using system.text;
 8 using system.windows.forms;
 9 
10 namespace hzh_controls.controls
11 {
12     public partial class ucleddatatime : usercontrol
13     {
14         private datetime m_value;
15 
16         [description("值"), category("自定义")]
17         public datetime value
18         {
19             get { return m_value; }
20             set
21             {
22                 m_value = value;
23                 string str = value.tostring("yyyy-mm-dd hh:mm:ss");
24                 for (int i = 0; i < str.length; i++)
25                 {
26                     if (i == 10)
27                         continue;
28                     ((uclednum)this.tablelayoutpanel1.controls.find("d" + (i + 1), false)[0]).value = str[i];
29                 }
30             }
31         }
32 
33         private int m_linewidth = 8;
34 
35         [description("线宽度,为了更好的显示效果,请使用偶数"), category("自定义")]
36         public int linewidth
37         {
38             get { return m_linewidth; }
39             set
40             {
41                 m_linewidth = value;
42                 foreach (uclednum c in this.tablelayoutpanel1.controls)
43                 {
44                     c.linewidth = value;
45                 }
46             }
47         }
48 
49         [description("颜色"), category("自定义")]
50         public override system.drawing.color forecolor
51         {
52             get
53             {
54                 return base.forecolor;
55             }
56             set
57             {
58                 base.forecolor = value;
59                 foreach (uclednum c in this.tablelayoutpanel1.controls)
60                 {
61                     c.forecolor = value;
62                 }
63             }
64         }
65         public ucleddatatime()
66         {
67             initializecomponent();
68             value = datetime.now;
69         }
70     }
71 }
  1 namespace hzh_controls.controls
  2 {
  3     partial class ucleddatatime
  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.tablelayoutpanel1 = new system.windows.forms.tablelayoutpanel();
 32             this.d1 = new hzh_controls.controls.uclednum();
 33             this.d2 = new hzh_controls.controls.uclednum();
 34             this.d3 = new hzh_controls.controls.uclednum();
 35             this.d4 = new hzh_controls.controls.uclednum();
 36             this.d5 = new hzh_controls.controls.uclednum();
 37             this.d6 = new hzh_controls.controls.uclednum();
 38             this.d7 = new hzh_controls.controls.uclednum();
 39             this.d8 = new hzh_controls.controls.uclednum();
 40             this.d9 = new hzh_controls.controls.uclednum();
 41             this.d10 = new hzh_controls.controls.uclednum();
 42             this.d12 = new hzh_controls.controls.uclednum();
 43             this.d13 = new hzh_controls.controls.uclednum();
 44             this.d14 = new hzh_controls.controls.uclednum();
 45             this.d15 = new hzh_controls.controls.uclednum();
 46             this.d16 = new hzh_controls.controls.uclednum();
 47             this.d17 = new hzh_controls.controls.uclednum();
 48             this.d18 = new hzh_controls.controls.uclednum();
 49             this.d19 = new hzh_controls.controls.uclednum();
 50             this.tablelayoutpanel1.suspendlayout();
 51             this.suspendlayout();
 52             // 
 53             // tablelayoutpanel1
 54             // 
 55             this.tablelayoutpanel1.columncount = 19;
 56             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 57             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 58             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 59             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 60             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 61             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 62             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 63             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 64             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 65             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 66             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 67             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 68             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 69             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 70             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 71             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 72             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 73             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 74             this.tablelayoutpanel1.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 5.263158f));
 75             this.tablelayoutpanel1.controls.add(this.d1, 0, 0);
 76             this.tablelayoutpanel1.controls.add(this.d2, 1, 0);
 77             this.tablelayoutpanel1.controls.add(this.d3, 2, 0);
 78             this.tablelayoutpanel1.controls.add(this.d4, 3, 0);
 79             this.tablelayoutpanel1.controls.add(this.d5, 4, 0);
 80             this.tablelayoutpanel1.controls.add(this.d6, 5, 0);
 81             this.tablelayoutpanel1.controls.add(this.d7, 6, 0);
 82             this.tablelayoutpanel1.controls.add(this.d8, 7, 0);
 83             this.tablelayoutpanel1.controls.add(this.d9, 8, 0);
 84             this.tablelayoutpanel1.controls.add(this.d10, 9, 0);
 85             this.tablelayoutpanel1.controls.add(this.d12, 11, 0);
 86             this.tablelayoutpanel1.controls.add(this.d13, 12, 0);
 87             this.tablelayoutpanel1.controls.add(this.d14, 13, 0);
 88             this.tablelayoutpanel1.controls.add(this.d15, 14, 0);
 89             this.tablelayoutpanel1.controls.add(this.d16, 15, 0);
 90             this.tablelayoutpanel1.controls.add(this.d17, 16, 0);
 91             this.tablelayoutpanel1.controls.add(this.d18, 17, 0);
 92             this.tablelayoutpanel1.controls.add(this.d19, 18, 0);
 93             this.tablelayoutpanel1.dock = system.windows.forms.dockstyle.fill;
 94             this.tablelayoutpanel1.location = new system.drawing.point(0, 0);
 95             this.tablelayoutpanel1.name = "tablelayoutpanel1";
 96             this.tablelayoutpanel1.rowcount = 1;
 97             this.tablelayoutpanel1.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 100f));
 98             this.tablelayoutpanel1.size = new system.drawing.size(650, 58);
 99             this.tablelayoutpanel1.tabindex = 1;
100             // 
101             // d1
102             // 
103             this.d1.dock = system.windows.forms.dockstyle.fill;
104             this.d1.linewidth = 8;
105             this.d1.location = new system.drawing.point(3, 3);
106             this.d1.name = "d1";
107             this.d1.size = new system.drawing.size(28, 52);
108             this.d1.tabindex = 0;
109             this.d1.value = '2';
110             // 
111             // d2
112             // 
113             this.d2.dock = system.windows.forms.dockstyle.fill;
114             this.d2.linewidth = 8;
115             this.d2.location = new system.drawing.point(37, 3);
116             this.d2.name = "d2";
117             this.d2.size = new system.drawing.size(28, 52);
118             this.d2.tabindex = 1;
119             this.d2.value = '0';
120             // 
121             // d3
122             // 
123             this.d3.dock = system.windows.forms.dockstyle.fill;
124             this.d3.linewidth = 8;
125             this.d3.location = new system.drawing.point(71, 3);
126             this.d3.name = "d3";
127             this.d3.size = new system.drawing.size(28, 52);
128             this.d3.tabindex = 2;
129             this.d3.value = '1';
130             // 
131             // d4
132             // 
133             this.d4.dock = system.windows.forms.dockstyle.fill;
134             this.d4.linewidth = 8;
135             this.d4.location = new system.drawing.point(105, 3);
136             this.d4.name = "d4";
137             this.d4.size = new system.drawing.size(28, 52);
138             this.d4.tabindex = 3;
139             this.d4.value = '9';
140             // 
141             // d5
142             // 
143             this.d5.dock = system.windows.forms.dockstyle.fill;
144             this.d5.linewidth = 8;
145             this.d5.location = new system.drawing.point(139, 3);
146             this.d5.name = "d5";
147             this.d5.size = new system.drawing.size(28, 52);
148             this.d5.tabindex = 4;
149             this.d5.value = '-';
150             // 
151             // d6
152             // 
153             this.d6.dock = system.windows.forms.dockstyle.fill;
154             this.d6.linewidth = 8;
155             this.d6.location = new system.drawing.point(173, 3);
156             this.d6.name = "d6";
157             this.d6.size = new system.drawing.size(28, 52);
158             this.d6.tabindex = 5;
159             this.d6.value = '0';
160             // 
161             // d7
162             // 
163             this.d7.dock = system.windows.forms.dockstyle.fill;
164             this.d7.linewidth = 8;
165             this.d7.location = new system.drawing.point(207, 3);
166             this.d7.name = "d7";
167             this.d7.size = new system.drawing.size(28, 52);
168             this.d7.tabindex = 6;
169             this.d7.value = '8';
170             // 
171             // d8
172             // 
173             this.d8.dock = system.windows.forms.dockstyle.fill;
174             this.d8.linewidth = 8;
175             this.d8.location = new system.drawing.point(241, 3);
176             this.d8.name = "d8";
177             this.d8.size = new system.drawing.size(28, 52);
178             this.d8.tabindex = 7;
179             this.d8.value = '-';
180             // 
181             // d9
182             // 
183             this.d9.dock = system.windows.forms.dockstyle.fill;
184             this.d9.linewidth = 8;
185             this.d9.location = new system.drawing.point(275, 3);
186             this.d9.name = "d9";
187             this.d9.size = new system.drawing.size(28, 52);
188             this.d9.tabindex = 8;
189             this.d9.value = '0';
190             // 
191             // d10
192             // 
193             this.d10.dock = system.windows.forms.dockstyle.fill;
194             this.d10.linewidth = 8;
195             this.d10.location = new system.drawing.point(309, 3);
196             this.d10.name = "d10";
197             this.d10.size = new system.drawing.size(28, 52);
198             this.d10.tabindex = 9;
199             this.d10.value = '1';
200             // 
201             // d12
202             // 
203             this.d12.dock = system.windows.forms.dockstyle.fill;
204             this.d12.linewidth = 8;
205             this.d12.location = new system.drawing.point(377, 3);
206             this.d12.name = "d12";
207             this.d12.size = new system.drawing.size(28, 52);
208             this.d12.tabindex = 10;
209             this.d12.value = '2';
210             // 
211             // d13
212             // 
213             this.d13.dock = system.windows.forms.dockstyle.fill;
214             this.d13.linewidth = 8;
215             this.d13.location = new system.drawing.point(411, 3);
216             this.d13.name = "d13";
217             this.d13.size = new system.drawing.size(28, 52);
218             this.d13.tabindex = 11;
219             this.d13.value = '3';
220             // 
221             // d14
222             // 
223             this.d14.dock = system.windows.forms.dockstyle.fill;
224             this.d14.linewidth = 8;
225             this.d14.location = new system.drawing.point(445, 3);
226             this.d14.name = "d14";
227             this.d14.size = new system.drawing.size(28, 52);
228             this.d14.tabindex = 12;
229             this.d14.value = ':';
230             // 
231             // d15
232             // 
233             this.d15.dock = system.windows.forms.dockstyle.fill;
234             this.d15.linewidth = 8;
235             this.d15.location = new system.drawing.point(479, 3);
236             this.d15.name = "d15";
237             this.d15.size = new system.drawing.size(28, 52);
238             this.d15.tabindex = 13;
239             this.d15.value = '0';
240             // 
241             // d16
242             // 
243             this.d16.dock = system.windows.forms.dockstyle.fill;
244             this.d16.linewidth = 8;
245             this.d16.location = new system.drawing.point(513, 3);
246             this.d16.name = "d16";
247             this.d16.size = new system.drawing.size(28, 52);
248             this.d16.tabindex = 14;
249             this.d16.value = '1';
250             // 
251             // d17
252             // 
253             this.d17.dock = system.windows.forms.dockstyle.fill;
254             this.d17.linewidth = 8;
255             this.d17.location = new system.drawing.point(547, 3);
256             this.d17.name = "d17";
257             this.d17.size = new system.drawing.size(28, 52);
258             this.d17.tabindex = 15;
259             this.d17.value = ':';
260             // 
261             // d18
262             // 
263             this.d18.dock = system.windows.forms.dockstyle.fill;
264             this.d18.linewidth = 8;
265             this.d18.location = new system.drawing.point(581, 3);
266             this.d18.name = "d18";
267             this.d18.size = new system.drawing.size(28, 52);
268             this.d18.tabindex = 16;
269             this.d18.value = '5';
270             // 
271             // d19
272             // 
273             this.d19.dock = system.windows.forms.dockstyle.fill;
274             this.d19.linewidth = 8;
275             this.d19.location = new system.drawing.point(615, 3);
276             this.d19.name = "d19";
277             this.d19.size = new system.drawing.size(32, 52);
278             this.d19.tabindex = 17;
279             this.d19.value = '3';
280             // 
281             // ucleddatatime
282             // 
283             this.autoscalemode = system.windows.forms.autoscalemode.none;
284             this.controls.add(this.tablelayoutpanel1);
285             this.name = "ucleddatatime";
286             this.size = new system.drawing.size(650, 58);
287             this.tablelayoutpanel1.resumelayout(false);
288             this.resumelayout(false);
289 
290         }
291 
292         #endregion
293 
294         private system.windows.forms.tablelayoutpanel tablelayoutpanel1;
295         private uclednum d1;
296         private uclednum d2;
297         private uclednum d3;
298         private uclednum d4;
299         private uclednum d5;
300         private uclednum d6;
301         private uclednum d7;
302         private uclednum d8;
303         private uclednum d9;
304         private uclednum d10;
305         private uclednum d12;
306         private uclednum d13;
307         private uclednum d14;
308         private uclednum d15;
309         private uclednum d16;
310         private uclednum d17;
311         private uclednum d18;
312         private uclednum d19;
313     }
314 }

 

最后的话

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