前提

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

开源地址:

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

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

目录

准备工作

日期控件将分为3部分进行处理,分别是,列表、日期面板、输入控件

将用到停靠窗体和基类控件,如你还没有了解,请移步查看

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

(一)c#winform自定义控件-基类控件

开始

添加用户控件,命名uctimepanel

属性

 1   public event eventhandler selectsourceevent;
 2         private list<keyvaluepair<string, string>> source = null;
 3         public bool firstevent { get; set; }
 4 
 5         public list<keyvaluepair<string, string>> source
 6         {
 7             get { return source; }
 8             set
 9             {
10                 source = value;
11                 setsource(value);
12             }
13         }
14 
15         private bool _isshowborder = false;
16 
17         public bool isshowborder
18         {
19             get { return _isshowborder; }
20             set
21             {
22                 _isshowborder = value;
23                 ucsplitline_h1.visible = value;
24                 ucsplitline_h2.visible = value;
25                 ucsplitline_v1.visible = value;
26                 ucsplitline_v2.visible = value;
27             }
28         }
29 
30         ucbtnext selectbtn;
31         /// <summary>
32         /// 选中按钮
33         /// </summary>
34         public ucbtnext selectbtn
35         {
36             get { return selectbtn; }
37             set
38             {
39                 if (selectbtn != null && !selectbtn.isdisposed)
40                 {
41                     selectbtn.fillcolor = system.drawing.color.white;
42                     selectbtn.rectcolor = system.drawing.color.white;
43                     selectbtn.btnforecolor = system.drawing.color.fromargb(66, 66, 66);
44                 }
45                 bool blnevent = firstevent ? true : (selectbtn != null);
46                 selectbtn = value;
47                 if (value != null)
48                 {
49                     selectbtn.fillcolor = system.drawing.color.fromargb(255, 77, 59);
50                     selectbtn.rectcolor = system.drawing.color.fromargb(255, 77, 59);
51                     selectbtn.btnforecolor = system.drawing.color.white;
52                     if (blnevent && selectsourceevent != null)
53                         selectsourceevent(selectbtn.tag.tostringext(), null);
54                 }
55             }
56         }
57  private int row = 0;
58 
59         public int row
60         {
61             get { return row; }
62             set
63             {
64                 row = value;
65                 reloadpanel();
66             }
67         }
68 
69 
70         private int column = 0;
71 
72         public int column
73         {
74             get { return column; }
75             set
76             {
77                 column = value;
78                 reloadpanel();
79             }
80         }

一些公共函数

  1         #region 设置面板数据源
  2         /// <summary>
  3         /// 功能描述:设置面板数据源
  4         /// 作  者:hzh
  5         /// 创建日期:2019-06-25 15:02:15
  6         /// 任务编号:pos
  7         /// </summary>
  8         /// <param name="lstsource">lstsource</param>
  9         public void setsource(list<keyvaluepair<string, string>> lstsource)
 10         {
 11             try
 12             {
 13                 controlhelper.freezecontrol(this, true);
 14                 if (row <= 0 || column <= 0)
 15                     return;
 16                 if (source != lstsource)
 17                     source = lstsource;
 18                 int index = 0;
 19                 selectbtn = null;
 20                 foreach (ucbtnext btn in this.panmain.controls)
 21                 {
 22                     if (lstsource != null && index < lstsource.count)
 23                     {
 24                         btn.btntext = lstsource[index].value;
 25                         btn.tag = lstsource[index].key;
 26                         index++;
 27                     }
 28                     else
 29                     {
 30                         btn.btntext = "";
 31                         btn.tag = null;
 32                     }
 33                 }
 34             }
 35             finally
 36             {
 37                 controlhelper.freezecontrol(this, false);
 38             }
 39         }
 40         #endregion
 41         /// <summary>
 42         /// 设置选中项
 43         /// </summary>
 44         /// <param name="strkey"></param>
 45         public void setselect(string strkey)
 46         {
 47             foreach (ucbtnext item in this.panmain.controls)
 48             {
 49                 if (item.tag != null && item.tag.tostringext() == strkey)
 50                 {
 51                     selectbtn = item;
 52                     return;
 53                 }
 54             }
 55             selectbtn = new ucbtnext();
 56         }
 57 
 58         #region 重置面板
 59         /// <summary>
 60         /// 功能描述:重置面板
 61         /// 作  者:hzh
 62         /// 创建日期:2019-06-25 15:02:05
 63         /// 任务编号:pos
 64         /// </summary>
 65         public void reloadpanel()
 66         {
 67             if (row <= 0 || column <= 0)
 68                 return;
 69             selectbtn = null;
 70             this.panmain.controls.clear();
 71             this.panmain.columncount = column;
 72             this.panmain.columnstyles.clear();
 73             for (int i = 0; i < column; i++)
 74             {
 75                 this.panmain.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 50f));
 76             }
 77 
 78             this.panmain.rowcount = row;
 79             this.panmain.rowstyles.clear();
 80             for (int i = 0; i < row; i++)
 81             {
 82                 this.panmain.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 50f));
 83             }
 84 
 85             for (int i = 0; i < row; i++)
 86             {
 87                 for (int j = 0; j < column; j++)
 88                 {
 89                     ucbtnext btn = new ucbtnext();
 90                     btn.backcolor = system.drawing.color.transparent;
 91                     btn.btnbackcolor = system.drawing.color.transparent;
 92                     btn.btnfont = new system.drawing.font("微软雅黑", 13f);
 93                     btn.btnforecolor = system.drawing.color.fromargb(66, 66, 66);
 94                     btn.conerradius = 5;
 95                     btn.dock = dockstyle.fill;
 96                     btn.fillcolor = system.drawing.color.white;
 97                     btn.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
 98                     btn.cursor = cursor.current;
 99                     btn.isshowrect = true;
100                     btn.isradius = true;
101                     btn.isshowtips = false;
102                     btn.name = "btn_" + i + "_" + j;
103                     btn.rectcolor = system.drawing.color.white;
104                     btn.rectwidth = 1;
105                     btn.width = this.width;
106                     btn.tabindex = 0;
107                     btn.tipstext = "";
108                     btn.btnclick += btn_btnclick;
109                     this.panmain.controls.add(btn, j, i);
110                 }
111             }
112 
113             if (source != null)
114             {
115                 setsource(source);
116             }
117         }
118         #endregion
119 
120         void btn_btnclick(object sender, eventargs e)
121         {
122             var btn = (ucbtnext)sender;
123             if (btn.tag == null)
124                 return;
125             selectbtn = btn;
126         }

全部代码

  1 // 版权所有  黄正辉  交流群:568015492   qq:623128629
2 // 文件名称:uctimepanel.cs
3 // 创建日期:2019-08-15 15:59:56
4 // 功能描述:datetime
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 
16 namespace hzh_controls.controls
17 {
18     [toolboxitem(false)]
19     public partial class uctimepanel : usercontrol
20     {
21         public event eventhandler selectsourceevent;
22         private list<keyvaluepair<string, string>> source = null;
23         public bool firstevent { get; set; }
24 
25         public list<keyvaluepair<string, string>> source
26         {
27             get { return source; }
28             set
29             {
30                 source = value;
31                 setsource(value);
32             }
33         }
34 
35         private bool _isshowborder = false;
36 
37         public bool isshowborder
38         {
39             get { return _isshowborder; }
40             set
41             {
42                 _isshowborder = value;
43                 ucsplitline_h1.visible = value;
44                 ucsplitline_h2.visible = value;
45                 ucsplitline_v1.visible = value;
46                 ucsplitline_v2.visible = value;
47             }
48         }
49 
50         ucbtnext selectbtn;
51         /// <summary>
52         /// 选中按钮
53         /// </summary>
54         public ucbtnext selectbtn
55         {
56             get { return selectbtn; }
57             set
58             {
59                 if (selectbtn != null && !selectbtn.isdisposed)
60                 {
61                     selectbtn.fillcolor = system.drawing.color.white;
62                     selectbtn.rectcolor = system.drawing.color.white;
63                     selectbtn.btnforecolor = system.drawing.color.fromargb(66, 66, 66);
64                 }
65                 bool blnevent = firstevent ? true : (selectbtn != null);
66                 selectbtn = value;
67                 if (value != null)
68                 {
69                     selectbtn.fillcolor = system.drawing.color.fromargb(255, 77, 59);
70                     selectbtn.rectcolor = system.drawing.color.fromargb(255, 77, 59);
71                     selectbtn.btnforecolor = system.drawing.color.white;
72                     if (blnevent && selectsourceevent != null)
73                         selectsourceevent(selectbtn.tag.tostringext(), null);
74                 }
75             }
76         }
77         public uctimepanel()
78         {
79             initializecomponent();
80             this.sizechanged += uctimepanel_sizechanged;
81         }
82 
83         void uctimepanel_sizechanged(object sender, eventargs e)
84         {
85 
86         }
87 
88         private int row = 0;
89 
90         public int row
91         {
92             get { return row; }
93             set
94             {
95                 row = value;
96                 reloadpanel();
97             }
98         }
99 
100 
101         private int column = 0;
102 
103         public int column
104         {
105             get { return column; }
106             set
107             {
108                 column = value;
109                 reloadpanel();
110             }
111         }
112 
113         private void uctimepanel_load(object sender, eventargs e)
114         {
115 
116         }
117 
118         #region 设置面板数据源
119         /// <summary>
120         /// 功能描述:设置面板数据源
121         /// 作  者:hzh
122         /// 创建日期:2019-06-25 15:02:15
123         /// 任务编号:pos
124         /// </summary>
125         /// <param name="lstsource">lstsource</param>
126         public void setsource(list<keyvaluepair<string, string>> lstsource)
127         {
128             try
129             {
130                 controlhelper.freezecontrol(this, true);
131                 if (row <= 0 || column <= 0)
132                     return;
133                 if (source != lstsource)
134                     source = lstsource;
135                 int index = 0;
136                 selectbtn = null;
137                 foreach (ucbtnext btn in this.panmain.controls)
138                 {
139                     if (lstsource != null && index < lstsource.count)
140                     {
141                         btn.btntext = lstsource[index].value;
142                         btn.tag = lstsource[index].key;
143                         index++;
144                     }
145                     else
146                     {
147                         btn.btntext = "";
148                         btn.tag = null;
149                     }
150                 }
151             }
152             finally
153             {
154                 controlhelper.freezecontrol(this, false);
155             }
156         }
157         #endregion
158         /// <summary>
159         /// 设置选中项
160         /// </summary>
161         /// <param name="strkey"></param>
162         public void setselect(string strkey)
163         {
164             foreach (ucbtnext item in this.panmain.controls)
165             {
166                 if (item.tag != null && item.tag.tostringext() == strkey)
167                 {
168                     selectbtn = item;
169                     return;
170                 }
171             }
172             selectbtn = new ucbtnext();
173         }
174 
175         #region 重置面板
176         /// <summary>
177         /// 功能描述:重置面板
178         /// 作  者:hzh
179         /// 创建日期:2019-06-25 15:02:05
180         /// 任务编号:pos
181         /// </summary>
182         public void reloadpanel()
183         {
184             if (row <= 0 || column <= 0)
185                 return;
186             selectbtn = null;
187             this.panmain.controls.clear();
188             this.panmain.columncount = column;
189             this.panmain.columnstyles.clear();
190             for (int i = 0; i < column; i++)
191             {
192                 this.panmain.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 50f));
193             }
194 
195             this.panmain.rowcount = row;
196             this.panmain.rowstyles.clear();
197             for (int i = 0; i < row; i++)
198             {
199                 this.panmain.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 50f));
200             }
201 
202             for (int i = 0; i < row; i++)
203             {
204                 for (int j = 0; j < column; j++)
205                 {
206                     ucbtnext btn = new ucbtnext();
207                     btn.backcolor = system.drawing.color.transparent;
208                     btn.btnbackcolor = system.drawing.color.transparent;
209                     btn.btnfont = new system.drawing.font("微软雅黑", 13f);
210                     btn.btnforecolor = system.drawing.color.fromargb(66, 66, 66);
211                     btn.conerradius = 5;
212                     btn.dock = dockstyle.fill;
213                     btn.fillcolor = system.drawing.color.white;
214                     btn.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
215                     btn.cursor = cursor.current;
216                     btn.isshowrect = true;
217                     btn.isradius = true;
218                     btn.isshowtips = false;
219                     btn.name = "btn_" + i + "_" + j;
220                     btn.rectcolor = system.drawing.color.white;
221                     btn.rectwidth = 1;
222                     btn.width = this.width;
223                     btn.tabindex = 0;
224                     btn.tipstext = "";
225                     btn.btnclick += btn_btnclick;
226                     this.panmain.controls.add(btn, j, i);
227                 }
228             }
229 
230             if (source != null)
231             {
232                 setsource(source);
233             }
234         }
235         #endregion
236 
237         void btn_btnclick(object sender, eventargs e)
238         {
239             var btn = (ucbtnext)sender;
240             if (btn.tag == null)
241                 return;
242             selectbtn = btn;
243         }
244     }
245 }
  1 namespace hzh_controls.controls
2 {
3     partial class uctimepanel
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.panmain = new system.windows.forms.tablelayoutpanel();
32             this.ucsplitline_v1 = new hzh_controls.controls.ucsplitline_v();
33             this.ucsplitline_v2 = new hzh_controls.controls.ucsplitline_v();
34             this.ucsplitline_h1 = new hzh_controls.controls.ucsplitline_h();
35             this.ucsplitline_h2 = new hzh_controls.controls.ucsplitline_h();
36             this.suspendlayout();
37             // 
38             // panmain
39             // 
40             this.panmain.columncount = 1;
41             this.panmain.columnstyles.add(new system.windows.forms.columnstyle(system.windows.forms.sizetype.percent, 50f));
42             this.panmain.dock = system.windows.forms.dockstyle.fill;
43             this.panmain.location = new system.drawing.point(1, 1);
44             this.panmain.name = "panmain";
45             this.panmain.rowcount = 1;
46             this.panmain.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.percent, 50f));
47             this.panmain.rowstyles.add(new system.windows.forms.rowstyle(system.windows.forms.sizetype.absolute, 20f));
48             this.panmain.size = new system.drawing.size(99, 228);
49             this.panmain.tabindex = 0;
50             // 
51             // ucsplitline_v1
52             // 
53             this.ucsplitline_v1.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
54             this.ucsplitline_v1.dock = system.windows.forms.dockstyle.left;
55             this.ucsplitline_v1.location = new system.drawing.point(0, 0);
56             this.ucsplitline_v1.name = "ucsplitline_v1";
57             this.ucsplitline_v1.size = new system.drawing.size(1, 230);
58             this.ucsplitline_v1.tabindex = 0;
59             this.ucsplitline_v1.tabstop = false;
60             this.ucsplitline_v1.visible = false;
61             // 
62             // ucsplitline_v2
63             // 
64             this.ucsplitline_v2.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
65             this.ucsplitline_v2.dock = system.windows.forms.dockstyle.right;
66             this.ucsplitline_v2.location = new system.drawing.point(100, 0);
67             this.ucsplitline_v2.name = "ucsplitline_v2";
68             this.ucsplitline_v2.size = new system.drawing.size(1, 230);
69             this.ucsplitline_v2.tabindex = 1;
70             this.ucsplitline_v2.tabstop = false;
71             this.ucsplitline_v2.visible = false;
72             // 
73             // ucsplitline_h1
74             // 
75             this.ucsplitline_h1.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
76             this.ucsplitline_h1.dock = system.windows.forms.dockstyle.top;
77             this.ucsplitline_h1.location = new system.drawing.point(1, 0);
78             this.ucsplitline_h1.name = "ucsplitline_h1";
79             this.ucsplitline_h1.size = new system.drawing.size(99, 1);
80             this.ucsplitline_h1.tabindex = 0;
81             this.ucsplitline_h1.tabstop = false;
82             this.ucsplitline_h1.visible = false;
83             // 
84             // ucsplitline_h2
85             // 
86             this.ucsplitline_h2.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
87             this.ucsplitline_h2.dock = system.windows.forms.dockstyle.bottom;
88             this.ucsplitline_h2.location = new system.drawing.point(1, 229);
89             this.ucsplitline_h2.name = "ucsplitline_h2";
90             this.ucsplitline_h2.size = new system.drawing.size(99, 1);
91             this.ucsplitline_h2.tabindex = 2;
92             this.ucsplitline_h2.tabstop = false;
93             this.ucsplitline_h2.visible = false;
94             // 
95             // uctimepanel
96             // 
97             this.autoscalemode = system.windows.forms.autoscalemode.none;
98             this.backcolor = system.drawing.color.white;
99             this.controls.add(this.panmain);
100             this.controls.add(this.ucsplitline_h2);
101             this.controls.add(this.ucsplitline_h1);
102             this.controls.add(this.ucsplitline_v2);
103             this.controls.add(this.ucsplitline_v1);
104             this.name = "uctimepanel";
105             this.size = new system.drawing.size(101, 230);
106             this.load += new system.eventhandler(this.uctimepanel_load);
107             this.resumelayout(false);
108 
109         }
110 
111         #endregion
112 
113         private system.windows.forms.tablelayoutpanel panmain;
114         private ucsplitline_v ucsplitline_v1;
115         private ucsplitline_v ucsplitline_v2;
116         private ucsplitline_h ucsplitline_h1;
117         private ucsplitline_h ucsplitline_h2;
118     }
119 }

 

下面是时间选择面板

添加用户控件,命名ucdatetimeselectpan

属性

 1   [description("确定事件"), category("自定义")]
2         public event eventhandler selectedtimeevent;
3         [description("取消事件"), category("自定义")]
4         public event eventhandler canceltimeevent;
5         private bool autoselectnext = true;
6         [description("自动选中下一级"), category("自定义")]
7         public bool autoselectnext
8         {
9             get { return autoselectnext; }
10             set { autoselectnext = value; }
11         }
12 
13         datetime m_dt = datetime.now;
14 
15         public datetime currenttime
16         {
17             get { return m_dt; }
18             set
19             {
20                 m_dt = value;
21                 settimetocontrol();
22             }
23         }
24         ucbtnext m_thisbtn = null;
25 
26         datetimepickertype m_type = datetimepickertype.datetime;
27         [description("时间类型"), category("自定义")]
28         public datetimepickertype timetype
29         {
30             get { return m_type; }
31             set { m_type = value; }
32         }

2个构造函数

 1  public ucdatetimeselectpan()
2         {
3             initializecomponent();
4             pantime.selectsourceevent += pantime_selectsourceevent;
5             this.tabstop = false;
6         }
7         public ucdatetimeselectpan(datetime dt)
8         {
9             m_dt = dt;
10             initializecomponent();
11             pantime.selectsourceevent += pantime_selectsourceevent;
12             this.tabstop = false;
13         }

一些事件

  1 void pantime_selectsourceevent(object sender, eventargs e)
2         {
3             string strkey = sender.tostring();
4             if (m_thisbtn == btnyear)
5             {
6                 m_dt = (strkey + "-" + m_dt.month + "-" + m_dt.day + " " + m_dt.hour + ":" + m_dt.minute).todate();
7             }
8             else if (m_thisbtn == btnmonth)
9             {
10                 m_dt = (m_dt.year + "-" + strkey + "-" + m_dt.day + " " + m_dt.hour + ":" + m_dt.minute).todate();
11             }
12             else if (m_thisbtn == btnday)
13             {
14                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + strkey + " " + m_dt.hour + ":" + m_dt.minute).todate();
15             }
16             else if (m_thisbtn == btnhour)
17             {
18                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + m_dt.day + " " + strkey + ":" + m_dt.minute).todate();
19             }
20             else if (m_thisbtn == btnminute)
21             {
22                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + m_dt.day + " " + m_dt.hour + ":" + strkey).todate();
23             }
24             settimetocontrol();
25             if (this.visible)
26             {
27                 if (autoselectnext)
28                 {
29                     if (m_thisbtn == btnyear)
30                     {
31                         setselecttype(btnmonth);
32                     }
33                     else if (m_thisbtn == btnmonth)
34                     {
35                         setselecttype(btnday);
36                     }
37                     else if (m_thisbtn == btnday)
38                     {
39                         if (m_type == datetimepickertype.datetime || m_type == datetimepickertype.time)
40                             setselecttype(btnhour);
41                     }
42                     else if (m_thisbtn == btnhour)
43                     {
44                         setselecttype(btnminute);
45                     }
46                 }
47             }
48         }
49 
50         private void ucdatetimepickerext_load(object sender, eventargs e)
51         {
52             settimetocontrol();
53 
54             if (m_type == datetimepickertype.date)
55             {
56                 btnhour.visible = false;
57                 btnminute.visible = false;
58             }
59             else if (m_type == datetimepickertype.time)
60             {
61                 btnyear.visible = false;
62                 btnmonth.visible = false;
63                 btnday.visible = false;
64                 sp1.visible = false;
65                 sp2.visible = false;
66                 sp3.visible = false;
67             }
68             if ((int)m_type <= 2)
69             {
70                 setselecttype(btnyear);
71             }
72             else
73             {
74                 setselecttype(btnhour);
75             }
76         }
77 
78         private void settimetocontrol()
79         {
80             btnyear.tag = m_dt.year;
81             btnyear.btntext = m_dt.year + "年";
82             btnmonth.tag = m_dt.month;
83             btnmonth.btntext = m_dt.month.tostring().padleft(2, '0') + "月";
84             btnday.tag = m_dt.day;
85             btnday.btntext = m_dt.day.tostring().padleft(2, '0') + "日";
86             btnhour.tag = m_dt.hour;
87             btnhour.btntext = m_dt.hour.tostring().padleft(2, '0') + "时";
88             btnminute.tag = m_dt.minute;
89             btnminute.btntext = m_dt.minute.tostring().padleft(2, '0') + "分";
90         }
91 
92         private void setselecttype(ucbtnext btn)
93         {
94             try
95             {
96                 controlhelper.freezecontrol(this, true);
97                 if (m_thisbtn != null)
98                 {
99                     m_thisbtn.fillcolor = color.white;
100                     m_thisbtn.btnforecolor = color.fromargb(255, 77, 59);
101                 }
102                 m_thisbtn = btn;
103                 if (m_thisbtn != null)
104                 {
105                     m_thisbtn.fillcolor = color.fromargb(255, 77, 59);
106                     m_thisbtn.btnforecolor = color.white;
107 
108                     list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
109                     pantime.suspendlayout();
110 
111                     if (btn == btnyear)
112                     {
113                         panleft.visible = true;
114                         panright.visible = true;
115                     }
116                     else
117                     {
118                         panleft.visible = false;
119                         panright.visible = false;
120                     }
121 
122                     if (btn == btnyear)
123                     {
124                         pantime.row = 5;
125                         pantime.column = 6;
126                         int intyear = m_dt.year - m_dt.year % 30;
127                         for (int i = 0; i < 30; i++)
128                         {
129                             lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
130                         }
131                     }
132                     else if (btn == btnmonth)
133                     {
134                         pantime.row = 3;
135                         pantime.column = 4;
136                         for (int i = 1; i <= 12; i++)
137                         {
138                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0') + "月\r\n" + (("2019-" + i + "-01").todate().tostring("mmm", system.globalization.cultureinfo.createspecificculture("en-gb")))));
139                         }
140                     }
141                     else if (btn == btnday)
142                     {
143                         pantime.column = 7;
144                         int intdaycount = datetime.daysinmonth(m_dt.year, m_dt.month);
145                         int intindex = (int)(m_dt.dayofweek);
146                         pantime.row = (intdaycount + intindex) / 7 + ((intdaycount + intindex) % 7 != 0 ? 1 : 0);
147                         for (int i = 0; i < intindex; i++)
148                         {
149                             lstsource.add(new keyvaluepair<string, string>("", ""));
150                         }
151                         for (int i = 1; i <= intdaycount; i++)
152                         {
153                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0')));
154                         }
155                     }
156                     else if (btn == btnhour)
157                     {
158                         pantime.row = 4;
159                         pantime.column = 6;
160                         for (int i = 0; i <= 24; i++)
161                         {
162                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring() + "时"));
163                         }
164                     }
165                     else if (btn == btnminute)
166                     {
167                         pantime.row = 5;
168                         pantime.column = 12;
169                         for (int i = 0; i <= 60; i++)
170                         {
171                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0')));
172                         }
173                     }
174                     pantime.source = lstsource;
175                     pantime.setselect(btn.tag.tostringext());
176                     pantime.resumelayout(true);
177 
178                     // pantime.enabled = true;
179                 }
180             }
181             finally
182             {
183                 controlhelper.freezecontrol(this, false);
184             }
185         }
186 
187         private void btntime_btnclick(object sender, eventargs e)
188         {
189             setselecttype((ucbtnext)sender);
190         }
191 
192         private void panleft_mousedown(object sender, mouseeventargs e)
193         {
194             list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
195             int intyear = this.pantime.source[0].key.toint() - this.pantime.source[0].key.toint() % 30 - 30;
196             pantime.suspendlayout();
197             pantime.row = 5;
198             pantime.column = 6;
199             for (int i = 0; i < 30; i++)
200             {
201                 lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
202             }
203             pantime.source = lstsource;
204             pantime.setselect(btnyear.tag.tostringext());
205             pantime.resumelayout(true);
206         }
207 
208         private void panright_mousedown(object sender, mouseeventargs e)
209         {
210             list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
211             int intyear = this.pantime.source[0].key.toint() - this.pantime.source[0].key.toint() % 30 + 30;
212             pantime.suspendlayout();
213             pantime.row = 5;
214             pantime.column = 6;
215             for (int i = 0; i < 30; i++)
216             {
217                 lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
218             }
219             pantime.source = lstsource;
220             pantime.setselect(btnyear.tag.tostringext());
221             pantime.resumelayout(true);
222         }
223 
224         private void btnok_btnclick(object sender, eventargs e)
225         {
226             if (selectedtimeevent != null)
227                 selectedtimeevent(m_dt, null);
228         }
229 
230         private void btncancel_btnclick(object sender, eventargs e)
231         {
232             if (canceltimeevent != null)
233             {
234                 canceltimeevent(null, null);
235             }
236         }

完整代码

  1 // 版权所有  黄正辉  交流群:568015492   qq:623128629
2 // 文件名称:ucdatetimeselectpan.cs
3 // 创建日期:2019-08-15 15:59:51
4 // 功能描述:datetime
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 
16 namespace hzh_controls.controls
17 {
18     [toolboxitem(false)]
19     public partial class ucdatetimeselectpan : usercontrol
20     {
21         [description("确定事件"), category("自定义")]
22         public event eventhandler selectedtimeevent;
23         [description("取消事件"), category("自定义")]
24         public event eventhandler canceltimeevent;
25         private bool autoselectnext = true;
26         [description("自动选中下一级"), category("自定义")]
27         public bool autoselectnext
28         {
29             get { return autoselectnext; }
30             set { autoselectnext = value; }
31         }
32 
33         datetime m_dt = datetime.now;
34 
35         public datetime currenttime
36         {
37             get { return m_dt; }
38             set
39             {
40                 m_dt = value;
41                 settimetocontrol();
42             }
43         }
44         ucbtnext m_thisbtn = null;
45 
46         datetimepickertype m_type = datetimepickertype.datetime;
47         [description("时间类型"), category("自定义")]
48         public datetimepickertype timetype
49         {
50             get { return m_type; }
51             set { m_type = value; }
52         }
53         public ucdatetimeselectpan()
54         {
55             initializecomponent();
56             pantime.selectsourceevent += pantime_selectsourceevent;
57             this.tabstop = false;
58         }
59         public ucdatetimeselectpan(datetime dt)
60         {
61             m_dt = dt;
62             initializecomponent();
63             pantime.selectsourceevent += pantime_selectsourceevent;
64             this.tabstop = false;
65         }
66 
67         void pantime_selectsourceevent(object sender, eventargs e)
68         {
69             string strkey = sender.tostring();
70             if (m_thisbtn == btnyear)
71             {
72                 m_dt = (strkey + "-" + m_dt.month + "-" + m_dt.day + " " + m_dt.hour + ":" + m_dt.minute).todate();
73             }
74             else if (m_thisbtn == btnmonth)
75             {
76                 m_dt = (m_dt.year + "-" + strkey + "-" + m_dt.day + " " + m_dt.hour + ":" + m_dt.minute).todate();
77             }
78             else if (m_thisbtn == btnday)
79             {
80                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + strkey + " " + m_dt.hour + ":" + m_dt.minute).todate();
81             }
82             else if (m_thisbtn == btnhour)
83             {
84                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + m_dt.day + " " + strkey + ":" + m_dt.minute).todate();
85             }
86             else if (m_thisbtn == btnminute)
87             {
88                 m_dt = (m_dt.year + "-" + m_dt.month + "-" + m_dt.day + " " + m_dt.hour + ":" + strkey).todate();
89             }
90             settimetocontrol();
91             if (this.visible)
92             {
93                 if (autoselectnext)
94                 {
95                     if (m_thisbtn == btnyear)
96                     {
97                         setselecttype(btnmonth);
98                     }
99                     else if (m_thisbtn == btnmonth)
100                     {
101                         setselecttype(btnday);
102                     }
103                     else if (m_thisbtn == btnday)
104                     {
105                         if (m_type == datetimepickertype.datetime || m_type == datetimepickertype.time)
106                             setselecttype(btnhour);
107                     }
108                     else if (m_thisbtn == btnhour)
109                     {
110                         setselecttype(btnminute);
111                     }
112                 }
113             }
114         }
115 
116         private void ucdatetimepickerext_load(object sender, eventargs e)
117         {
118             settimetocontrol();
119 
120             if (m_type == datetimepickertype.date)
121             {
122                 btnhour.visible = false;
123                 btnminute.visible = false;
124             }
125             else if (m_type == datetimepickertype.time)
126             {
127                 btnyear.visible = false;
128                 btnmonth.visible = false;
129                 btnday.visible = false;
130                 sp1.visible = false;
131                 sp2.visible = false;
132                 sp3.visible = false;
133             }
134             if ((int)m_type <= 2)
135             {
136                 setselecttype(btnyear);
137             }
138             else
139             {
140                 setselecttype(btnhour);
141             }
142         }
143 
144         private void settimetocontrol()
145         {
146             btnyear.tag = m_dt.year;
147             btnyear.btntext = m_dt.year + "年";
148             btnmonth.tag = m_dt.month;
149             btnmonth.btntext = m_dt.month.tostring().padleft(2, '0') + "月";
150             btnday.tag = m_dt.day;
151             btnday.btntext = m_dt.day.tostring().padleft(2, '0') + "日";
152             btnhour.tag = m_dt.hour;
153             btnhour.btntext = m_dt.hour.tostring().padleft(2, '0') + "时";
154             btnminute.tag = m_dt.minute;
155             btnminute.btntext = m_dt.minute.tostring().padleft(2, '0') + "分";
156         }
157 
158         private void setselecttype(ucbtnext btn)
159         {
160             try
161             {
162                 controlhelper.freezecontrol(this, true);
163                 if (m_thisbtn != null)
164                 {
165                     m_thisbtn.fillcolor = color.white;
166                     m_thisbtn.btnforecolor = color.fromargb(255, 77, 59);
167                 }
168                 m_thisbtn = btn;
169                 if (m_thisbtn != null)
170                 {
171                     m_thisbtn.fillcolor = color.fromargb(255, 77, 59);
172                     m_thisbtn.btnforecolor = color.white;
173 
174                     list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
175                     pantime.suspendlayout();
176 
177                     if (btn == btnyear)
178                     {
179                         panleft.visible = true;
180                         panright.visible = true;
181                     }
182                     else
183                     {
184                         panleft.visible = false;
185                         panright.visible = false;
186                     }
187 
188                     if (btn == btnyear)
189                     {
190                         pantime.row = 5;
191                         pantime.column = 6;
192                         int intyear = m_dt.year - m_dt.year % 30;
193                         for (int i = 0; i < 30; i++)
194                         {
195                             lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
196                         }
197                     }
198                     else if (btn == btnmonth)
199                     {
200                         pantime.row = 3;
201                         pantime.column = 4;
202                         for (int i = 1; i <= 12; i++)
203                         {
204                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0') + "月\r\n" + (("2019-" + i + "-01").todate().tostring("mmm", system.globalization.cultureinfo.createspecificculture("en-gb")))));
205                         }
206                     }
207                     else if (btn == btnday)
208                     {
209                         pantime.column = 7;
210                         int intdaycount = datetime.daysinmonth(m_dt.year, m_dt.month);
211                         int intindex = (int)(m_dt.dayofweek);
212                         pantime.row = (intdaycount + intindex) / 7 + ((intdaycount + intindex) % 7 != 0 ? 1 : 0);
213                         for (int i = 0; i < intindex; i++)
214                         {
215                             lstsource.add(new keyvaluepair<string, string>("", ""));
216                         }
217                         for (int i = 1; i <= intdaycount; i++)
218                         {
219                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0')));
220                         }
221                     }
222                     else if (btn == btnhour)
223                     {
224                         pantime.row = 4;
225                         pantime.column = 6;
226                         for (int i = 0; i <= 24; i++)
227                         {
228                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring() + "时"));
229                         }
230                     }
231                     else if (btn == btnminute)
232                     {
233                         pantime.row = 5;
234                         pantime.column = 12;
235                         for (int i = 0; i <= 60; i++)
236                         {
237                             lstsource.add(new keyvaluepair<string, string>(i.tostring(), i.tostring().padleft(2, '0')));
238                         }
239                     }
240                     pantime.source = lstsource;
241                     pantime.setselect(btn.tag.tostringext());
242                     pantime.resumelayout(true);
243 
244                     // pantime.enabled = true;
245                 }
246             }
247             finally
248             {
249                 controlhelper.freezecontrol(this, false);
250             }
251         }
252 
253         private void btntime_btnclick(object sender, eventargs e)
254         {
255             setselecttype((ucbtnext)sender);
256         }
257 
258         private void panleft_mousedown(object sender, mouseeventargs e)
259         {
260             list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
261             int intyear = this.pantime.source[0].key.toint() - this.pantime.source[0].key.toint() % 30 - 30;
262             pantime.suspendlayout();
263             pantime.row = 5;
264             pantime.column = 6;
265             for (int i = 0; i < 30; i++)
266             {
267                 lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
268             }
269             pantime.source = lstsource;
270             pantime.setselect(btnyear.tag.tostringext());
271             pantime.resumelayout(true);
272         }
273 
274         private void panright_mousedown(object sender, mouseeventargs e)
275         {
276             list<keyvaluepair<string, string>> lstsource = new list<keyvaluepair<string, string>>();
277             int intyear = this.pantime.source[0].key.toint() - this.pantime.source[0].key.toint() % 30 + 30;
278             pantime.suspendlayout();
279             pantime.row = 5;
280             pantime.column = 6;
281             for (int i = 0; i < 30; i++)
282             {
283                 lstsource.add(new keyvaluepair<string, string>((intyear + i).tostring(), (intyear + i).tostring()));
284             }
285             pantime.source = lstsource;
286             pantime.setselect(btnyear.tag.tostringext());
287             pantime.resumelayout(true);
288         }
289 
290         private void btnok_btnclick(object sender, eventargs e)
291         {
292             if (selectedtimeevent != null)
293                 selectedtimeevent(m_dt, null);
294         }
295 
296         private void btncancel_btnclick(object sender, eventargs e)
297         {
298             if (canceltimeevent != null)
299             {
300                 canceltimeevent(null, null);
301             }
302         }
303     }
304 }
  1 namespace hzh_controls.controls
2 {
3     public partial class ucdatetimeselectpan
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.panel1 = new system.windows.forms.panel();
32             this.btnminute = new hzh_controls.controls.ucbtnext();
33             this.sp4 = new system.windows.forms.panel();
34             this.btnhour = new hzh_controls.controls.ucbtnext();
35             this.sp3 = new system.windows.forms.panel();
36             this.btnday = new hzh_controls.controls.ucbtnext();
37             this.sp2 = new system.windows.forms.panel();
38             this.btnmonth = new hzh_controls.controls.ucbtnext();
39             this.sp1 = new system.windows.forms.panel();
40             this.btnyear = new hzh_controls.controls.ucbtnext();
41             this.panel2 = new system.windows.forms.panel();
42             this.btncancel = new hzh_controls.controls.ucbtnext();
43             this.btnok = new hzh_controls.controls.ucbtnext();
44             this.panmian = new system.windows.forms.panel();
45             this.pantime = new hzh_controls.controls.uctimepanel();
46             this.panright = new system.windows.forms.panel();
47             this.panleft = new system.windows.forms.panel();
48             this.ucsplitline_h2 = new hzh_controls.controls.ucsplitline_h();
49             this.ucsplitline_h1 = new hzh_controls.controls.ucsplitline_h();
50             this.panel3 = new system.windows.forms.panel();
51             this.panel1.suspendlayout();
52             this.panel2.suspendlayout();
53             this.panmian.suspendlayout();
54             this.panel3.suspendlayout();
55             this.suspendlayout();
56             // 
57             // panel1
58             // 
59             this.panel1.controls.add(this.btnminute);
60             this.panel1.controls.add(this.sp4);
61             this.panel1.controls.add(this.btnhour);
62             this.panel1.controls.add(this.sp3);
63             this.panel1.controls.add(this.btnday);
64             this.panel1.controls.add(this.sp2);
65             this.panel1.controls.add(this.btnmonth);
66             this.panel1.controls.add(this.sp1);
67             this.panel1.controls.add(this.btnyear);
68             this.panel1.dock = system.windows.forms.dockstyle.top;
69             this.panel1.location = new system.drawing.point(9, 9);
70             this.panel1.name = "panel1";
71             this.panel1.padding = new system.windows.forms.padding(10, 7, 10, 7);
72             this.panel1.size = new system.drawing.size(497, 45);
73             this.panel1.tabindex = 0;
74             // 
75             // btnminute
76             // 
77             this.btnminute.backcolor = system.drawing.color.transparent;
78             this.btnminute.btnbackcolor = system.drawing.color.transparent;
79             this.btnminute.btnfont = new system.drawing.font("微软雅黑", 12f);
80             this.btnminute.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
81             this.btnminute.btntext = "30分";
82             this.btnminute.conerradius = 5;
83             this.btnminute.cursor = system.windows.forms.cursors.hand;
84             this.btnminute.dock = system.windows.forms.dockstyle.left;
85             this.btnminute.fillcolor = system.drawing.color.white;
86             this.btnminute.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
87             this.btnminute.isradius = true;
88             this.btnminute.isshowrect = true;
89             this.btnminute.isshowtips = false;
90             this.btnminute.location = new system.drawing.point(406, 7);
91             this.btnminute.margin = new system.windows.forms.padding(3, 6, 3, 6);
92             this.btnminute.name = "btnminute";
93             this.btnminute.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
94             this.btnminute.rectwidth = 1;
95             this.btnminute.size = new system.drawing.size(80, 31);
96             this.btnminute.tabindex = 1;
97             this.btnminute.tabstop = false;
98             this.btnminute.tipstext = "";
99             this.btnminute.btnclick += new system.eventhandler(this.btntime_btnclick);
100             // 
101             // sp4
102             // 
103             this.sp4.dock = system.windows.forms.dockstyle.left;
104             this.sp4.location = new system.drawing.point(387, 7);
105             this.sp4.name = "sp4";
106             this.sp4.size = new system.drawing.size(19, 31);
107             this.sp4.tabindex = 5;
108             // 
109             // btnhour
110             // 
111             this.btnhour.backcolor = system.drawing.color.transparent;
112             this.btnhour.btnbackcolor = system.drawing.color.transparent;
113             this.btnhour.btnfont = new system.drawing.font("微软雅黑", 12f);
114             this.btnhour.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
115             this.btnhour.btntext = "12时";
116             this.btnhour.conerradius = 5;
117             this.btnhour.cursor = system.windows.forms.cursors.hand;
118             this.btnhour.dock = system.windows.forms.dockstyle.left;
119             this.btnhour.fillcolor = system.drawing.color.white;
120             this.btnhour.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
121             this.btnhour.isradius = true;
122             this.btnhour.isshowrect = true;
123             this.btnhour.isshowtips = false;
124             this.btnhour.location = new system.drawing.point(307, 7);
125             this.btnhour.margin = new system.windows.forms.padding(3, 6, 3, 6);
126             this.btnhour.name = "btnhour";
127             this.btnhour.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
128             this.btnhour.rectwidth = 1;
129             this.btnhour.size = new system.drawing.size(80, 31);
130             this.btnhour.tabindex = 1;
131             this.btnhour.tabstop = false;
132             this.btnhour.tipstext = "";
133             this.btnhour.btnclick += new system.eventhandler(this.btntime_btnclick);
134             // 
135             // sp3
136             // 
137             this.sp3.dock = system.windows.forms.dockstyle.left;
138             this.sp3.location = new system.drawing.point(288, 7);
139             this.sp3.name = "sp3";
140             this.sp3.size = new system.drawing.size(19, 31);
141             this.sp3.tabindex = 4;
142             // 
143             // btnday
144             // 
145             this.btnday.backcolor = system.drawing.color.transparent;
146             this.btnday.btnbackcolor = system.drawing.color.transparent;
147             this.btnday.btnfont = new system.drawing.font("微软雅黑", 12f);
148             this.btnday.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
149             this.btnday.btntext = "30日";
150             this.btnday.conerradius = 5;
151             this.btnday.cursor = system.windows.forms.cursors.hand;
152             this.btnday.dock = system.windows.forms.dockstyle.left;
153             this.btnday.fillcolor = system.drawing.color.white;
154             this.btnday.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
155             this.btnday.isradius = true;
156             this.btnday.isshowrect = true;
157             this.btnday.isshowtips = false;
158             this.btnday.location = new system.drawing.point(208, 7);
159             this.btnday.margin = new system.windows.forms.padding(3, 6, 3, 6);
160             this.btnday.name = "btnday";
161             this.btnday.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
162             this.btnday.rectwidth = 1;
163             this.btnday.size = new system.drawing.size(80, 31);
164             this.btnday.tabindex = 1;
165             this.btnday.tabstop = false;
166             this.btnday.tipstext = "";
167             this.btnday.btnclick += new system.eventhandler(this.btntime_btnclick);
168             // 
169             // sp2
170             // 
171             this.sp2.dock = system.windows.forms.dockstyle.left;
172             this.sp2.location = new system.drawing.point(189, 7);
173             this.sp2.name = "sp2";
174             this.sp2.size = new system.drawing.size(19, 31);
175             this.sp2.tabindex = 3;
176             // 
177             // btnmonth
178             // 
179             this.btnmonth.backcolor = system.drawing.color.transparent;
180             this.btnmonth.btnbackcolor = system.drawing.color.transparent;
181             this.btnmonth.btnfont = new system.drawing.font("微软雅黑", 12f);
182             this.btnmonth.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
183             this.btnmonth.btntext = "12月";
184             this.btnmonth.conerradius = 5;
185             this.btnmonth.cursor = system.windows.forms.cursors.hand;
186             this.btnmonth.dock = system.windows.forms.dockstyle.left;
187             this.btnmonth.fillcolor = system.drawing.color.white;
188             this.btnmonth.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
189             this.btnmonth.isradius = true;
190             this.btnmonth.isshowrect = true;
191             this.btnmonth.isshowtips = false;
192             this.btnmonth.location = new system.drawing.point(109, 7);
193             this.btnmonth.margin = new system.windows.forms.padding(3, 6, 3, 6);
194             this.btnmonth.name = "btnmonth";
195             this.btnmonth.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
196             this.btnmonth.rectwidth = 1;
197             this.btnmonth.size = new system.drawing.size(80, 31);
198             this.btnmonth.tabindex = 1;
199             this.btnmonth.tabstop = false;
200             this.btnmonth.tipstext = "";
201             this.btnmonth.btnclick += new system.eventhandler(this.btntime_btnclick);
202             // 
203             // sp1
204             // 
205             this.sp1.dock = system.windows.forms.dockstyle.left;
206             this.sp1.location = new system.drawing.point(90, 7);
207             this.sp1.name = "sp1";
208             this.sp1.size = new system.drawing.size(19, 31);
209             this.sp1.tabindex = 2;
210             // 
211             // btnyear
212             // 
213             this.btnyear.backcolor = system.drawing.color.transparent;
214             this.btnyear.btnbackcolor = system.drawing.color.transparent;
215             this.btnyear.btnfont = new system.drawing.font("微软雅黑", 12f);
216             this.btnyear.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
217             this.btnyear.btntext = "2019年";
218             this.btnyear.conerradius = 5;
219             this.btnyear.cursor = system.windows.forms.cursors.hand;
220             this.btnyear.dock = system.windows.forms.dockstyle.left;
221             this.btnyear.fillcolor = system.drawing.color.white;
222             this.btnyear.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
223             this.btnyear.isradius = true;
224             this.btnyear.isshowrect = true;
225             this.btnyear.isshowtips = false;
226             this.btnyear.location = new system.drawing.point(10, 7);
227             this.btnyear.margin = new system.windows.forms.padding(3, 6, 3, 6);
228             this.btnyear.name = "btnyear";
229             this.btnyear.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
230             this.btnyear.rectwidth = 1;
231             this.btnyear.size = new system.drawing.size(80, 31);
232             this.btnyear.tabindex = 1;
233             this.btnyear.tabstop = false;
234             this.btnyear.tipstext = "";
235             this.btnyear.btnclick += new system.eventhandler(this.btntime_btnclick);
236             // 
237             // panel2
238             // 
239             this.panel2.controls.add(this.btncancel);
240             this.panel2.controls.add(this.btnok);
241             this.panel2.dock = system.windows.forms.dockstyle.bottom;
242             this.panel2.location = new system.drawing.point(9, 300);
243             this.panel2.name = "panel2";
244             this.panel2.size = new system.drawing.size(497, 54);
245             this.panel2.tabindex = 2;
246             // 
247             // btncancel
248             // 
249             this.btncancel.anchor = system.windows.forms.anchorstyles.none;
250             this.btncancel.backcolor = system.drawing.color.transparent;
251             this.btncancel.btnbackcolor = system.drawing.color.transparent;
252             this.btncancel.btnfont = new system.drawing.font("微软雅黑", 13f);
253             this.btncancel.btnforecolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
254             this.btncancel.btntext = "取   消";
255             this.btncancel.conerradius = 5;
256             this.btncancel.cursor = system.windows.forms.cursors.hand;
257             this.btncancel.fillcolor = system.drawing.color.white;
258             this.btncancel.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
259             this.btncancel.isradius = true;
260             this.btncancel.isshowrect = true;
261             this.btncancel.isshowtips = false;
262             this.btncancel.location = new system.drawing.point(89, 9);
263             this.btncancel.margin = new system.windows.forms.padding(3, 6, 3, 6);
264             this.btncancel.name = "btncancel";
265             this.btncancel.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
266             this.btncancel.rectwidth = 1;
267             this.btncancel.size = new system.drawing.size(129, 36);
268             this.btncancel.tabindex = 2;
269             this.btncancel.tabstop = false;
270             this.btncancel.tipstext = "";
271             this.btncancel.btnclick += new system.eventhandler(this.btncancel_btnclick);
272             // 
273             // btnok
274             // 
275             this.btnok.anchor = system.windows.forms.anchorstyles.none;
276             this.btnok.backcolor = system.drawing.color.transparent;
277             this.btnok.btnbackcolor = system.drawing.color.transparent;
278             this.btnok.btnfont = new system.drawing.font("微软雅黑", 13f);
279             this.btnok.btnforecolor = system.drawing.color.white;
280             this.btnok.btntext = "确  定";
281             this.btnok.conerradius = 5;
282             this.btnok.cursor = system.windows.forms.cursors.hand;
283             this.btnok.fillcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
284             this.btnok.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
285             this.btnok.isradius = true;
286             this.btnok.isshowrect = true;
287             this.btnok.isshowtips = false;
288             this.btnok.location = new system.drawing.point(307, 9);
289             this.btnok.margin = new system.windows.forms.padding(3, 6, 3, 6);
290             this.btnok.name = "btnok";
291             this.btnok.rectcolor = system.drawing.color.fromargb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
292             this.btnok.rectwidth = 1;
293             this.btnok.size = new system.drawing.size(129, 36);
294             this.btnok.tabindex = 1;
295             this.btnok.tabstop = false;
296             this.btnok.tipstext = "";
297             this.btnok.btnclick += new system.eventhandler(this.btnok_btnclick);
298             // 
299             // panmian
300             // 
301             this.panmian.controls.add(this.pantime);
302             this.panmian.controls.add(this.panright);
303             this.panmian.controls.add(this.panleft);
304             this.panmian.dock = system.windows.forms.dockstyle.fill;
305             this.panmian.location = new system.drawing.point(9, 55);
306             this.panmian.name = "panmian";
307             this.panmian.size = new system.drawing.size(497, 244);
308             this.panmian.tabindex = 4;
309             // 
310             // pantime
311             // 
312             this.pantime.backcolor = system.drawing.color.white;
313             this.pantime.column = 0;
314             this.pantime.dock = system.windows.forms.dockstyle.fill;
315             this.pantime.firstevent = false;
316             this.pantime.location = new system.drawing.point(48, 0);
317             this.pantime.name = "pantime";
318             this.pantime.row = 0;
319             this.pantime.selectbtn = null;
320             this.pantime.size = new system.drawing.size(401, 244);
321             this.pantime.source = null;
322             this.pantime.tabindex = 0;
323             // 
324             // panright
325             // 
326             this.panright.backgroundimage = global::hzh_controls.properties.resources.dateright;
327             this.panright.backgroundimagelayout = system.windows.forms.imagelayout.center;
328             this.panright.dock = system.windows.forms.dockstyle.right;
329             this.panright.location = new system.drawing.point(449, 0);
330             this.panright.name = "panright";
331             this.panright.size = new system.drawing.size(48, 244);
332             this.panright.tabindex = 2;
333             this.panright.visible = false;
334             this.panright.mousedown += new system.windows.forms.mouseeventhandler(this.panright_mousedown);
335             // 
336             // panleft
337             // 
338             this.panleft.backgroundimage = global::hzh_controls.properties.resources.datetleft;
339             this.panleft.backgroundimagelayout = system.windows.forms.imagelayout.center;
340             this.panleft.dock = system.windows.forms.dockstyle.left;
341             this.panleft.location = new system.drawing.point(0, 0);
342             this.panleft.name = "panleft";
343             this.panleft.size = new system.drawing.size(48, 244);
344             this.panleft.tabindex = 1;
345             this.panleft.visible = false;
346             this.panleft.mousedown += new system.windows.forms.mouseeventhandler(this.panleft_mousedown);
347             // 
348             // ucsplitline_h2
349             // 
350             this.ucsplitline_h2.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
351             this.ucsplitline_h2.dock = system.windows.forms.dockstyle.bottom;
352             this.ucsplitline_h2.location = new system.drawing.point(9, 299);
353             this.ucsplitline_h2.name = "ucsplitline_h2";
354             this.ucsplitline_h2.size = new system.drawing.size(497, 1);
355             this.ucsplitline_h2.tabindex = 3;
356             this.ucsplitline_h2.tabstop = false;
357             // 
358             // ucsplitline_h1
359             // 
360             this.ucsplitline_h1.backcolor = system.drawing.color.fromargb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
361             this.ucsplitline_h1.dock = system.windows.forms.dockstyle.top;
362             this.ucsplitline_h1.location = new system.drawing.point(9, 54);
363             this.ucsplitline_h1.name = "ucsplitline_h1";
364             this.ucsplitline_h1.size = new system.drawing.size(497, 1);
365             this.ucsplitline_h1.tabindex = 1;
366             this.ucsplitline_h1.tabstop = false;
367             // 
368             // panel3
369             // 
370             this.panel3.backcolor = system.drawing.color.white;
371             this.panel3.controls.add(this.panmian);
372             this.panel3.controls.add(this.ucsplitline_h2);
373             this.panel3.controls.add(this.panel2);
374             this.panel3.controls.add(this.ucsplitline_h1);
375             this.panel3.controls.add(this.panel1);
376             this.panel3.dock = system.windows.forms.dockstyle.fill;
377             this.panel3.location = new system.drawing.point(1, 1);
378             this.panel3.name = "panel3";
379             this.panel3.padding = new system.windows.forms.padding(9);
380             this.panel3.size = new system.drawing.size(515, 363);
381             this.panel3.tabindex = 5;
382             // 
383             // ucdatetimeselectpan
384             // 
385             this.autoscalemode = system.windows.forms.autoscalemode.none;
386             this.backcolor = system.drawing.color.fromargb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
387             this.controls.add(this.panel3);
388             this.name = "ucdatetimeselectpan";
389             this.padding = new system.windows.forms.padding(1);
390             this.size = new system.drawing.size(517, 365);
391             this.load += new system.eventhandler(this.ucdatetimepickerext_load);
392             this.panel1.resumelayout(false);
393             this.panel2.resumelayout(false);
394             this.panmian.resumelayout(false);
395             this.panel3.resumelayout(false);
396             this.resumelayout(false);
397 
398         }
399 
400         #endregion
401 
402         private system.windows.forms.panel panel1;
403         private ucsplitline_h ucsplitline_h1;
404         private system.windows.forms.panel panel2;
405         private ucsplitline_h ucsplitline_h2;
406         private system.windows.forms.panel panmian;
407         private ucbtnext btnminute;
408         private ucbtnext btnday;
409         private ucbtnext btnhour;
410         private ucbtnext btnmonth;
411         private ucbtnext btnyear;
412         private uctimepanel pantime;
413         private ucbtnext btncancel;
414         private ucbtnext btnok;
415         private system.windows.forms.panel panright;
416         private system.windows.forms.panel panleft;
417         private system.windows.forms.panel sp4;
418         private system.windows.forms.panel sp3;
419         private system.windows.forms.panel sp2;
420         private system.windows.forms.panel sp1;
421         private system.windows.forms.panel panel3;
422 
423     }
424 }

 

日期输入控件

添加一个用户控件,命名ucdatepickerext,继承基类控件uccontrolbase

属性

 1  forms.frmanchor m_frmanchor;
2         ucdatetimeselectpan m_selectpan = null;
3         datetimepickertype m_type = datetimepickertype.datetime;
4         [description("时间类型"), category("自定义")]
5         public datetimepickertype timetype
6         {
7             get { return m_type; }
8             set
9             {
10                 m_type = value;
11                 if (value == datetimepickertype.datetime)
12                 {
13                     txtyear.visible = true;
14                     label1.visible = true;
15                     txtmonth.visible = true;
16                     label2.visible = true;
17                     txtday.visible = true;
18                     label3.visible = true;
19                     txthour.visible = true;
20                     label4.visible = true;
21                     txtminute.visible = true;
22                     label5.visible = true;
23                 }
24                 else if (value == datetimepickertype.date)
25                 {
26                     txtyear.visible = true;
27                     label1.visible = true;
28                     txtmonth.visible = true;
29                     label2.visible = true;
30                     txtday.visible = true;
31                     label3.visible = true;
32                     txthour.visible = false;
33                     label4.visible = false;
34                     txtminute.visible = false;
35                     label5.visible = false;
36                 }
37                 else
38                 {
39                     txtyear.visible = false;
40                     label1.visible = false;
41                     txtmonth.visible = false;
42                     label2.visible = false;
43                     txtday.visible = false;
44                     label3.visible = false;
45                     txthour.visible = true;
46                     label4.visible = true;
47                     txtminute.visible = true;
48                     label5.visible = true;
49                 }
50             }
51         }
52 
53         private datetime currenttime = datetime.now;
54 
55         private int timefontsize = 20;
56         [description("时间字体大小"), category("自定义")]
57         public int timefontsize
58         {
59             get { return timefontsize; }
60             set
61             {
62                 if (timefontsize != value)
63                 {
64                     timefontsize = value;
65                     foreach (control c in panel1.controls)
66                     {
67                         c.font = new font(c.font.name, value);
68                     }
69                 }
70             }
71         }
72 
73         [description("时间"), category("自定义")]
74         public datetime currenttime
75         {
76             get { return currenttime; }
77             set
78             {
79                 currenttime = value;
80                 settimetocontrol();
81             }
82         }

一些函数

 private void settimetocontrol()
{
this.txtyear.text = currenttime.year.tostring();
this.txtmonth.text = currenttime.month.tostring().padleft(2, '0');
this.txtday.text = currenttime.day.tostring().padleft(2, '0');
this.txthour.text = currenttime.hour.tostring().padleft(2, '0');
this.txtminute.text = currenttime.minute.tostring().padleft(2, '0');
}
private void ucdatepickerext_load(object sender, eventargs e)
{
settimetocontrol();
panel1.height = this.txtday.height;
panel1.height = this.txthour.height;
setevent(this);
}
private void setevent(control c)
{
if (c != null)
{
c.mousedown += c_mousedown;
foreach (control item in c.controls)
{
setevent(item);
}
}
}

一些事件

  1 void c_mousedown(object sender, mouseeventargs e)
2         {
3             if (m_selectpan == null)
4             {
5                 m_selectpan = new ucdatetimeselectpan();
6                 m_selectpan.selectedtimeevent += uc_selectedtimeevent;
7                 m_selectpan.canceltimeevent += m_selectpan_canceltimeevent;
8             }
9             m_selectpan.currenttime = currenttime;
10             m_selectpan.timetype = m_type;
11             m_frmanchor = new forms.frmanchor(this, m_selectpan);
12             m_frmanchor.show(this.findform());
13         }
14 
15         void m_selectpan_canceltimeevent(object sender, eventargs e)
16         {
17             m_frmanchor.hide();
18         }
19 
20         void uc_selectedtimeevent(object sender, eventargs e)
21         {
22             currenttime = m_selectpan.currenttime;
23             m_frmanchor.hide();
24         }
25 
26         private void txtyear_textchanged(object sender, eventargs e)
27         {
28             if (txtyear.text.length == 4)
29                 this.activecontrol = txtmonth;
30         }
31 
32         private void txtmonth_textchanged(object sender, eventargs e)
33         {
34             if (txtmonth.text.length == 2 || txtmonth.text.toint() >= 3)
35             {
36                 this.activecontrol = txtday;
37             }
38         }
39 
40         private void txtday_textchanged(object sender, eventargs e)
41         {
42             if (m_type == datetimepickertype.date)
43                 return;
44             if (txtday.text.length == 2 || txtday.text.toint() >= 4)
45             {
46                 this.activecontrol = txthour;
47             }
48         }
49 
50         private void txthour_textchanged(object sender, eventargs e)
51         {
52             if (txthour.text.length == 2 || txthour.text.toint() >= 3)
53             {
54                 this.activecontrol = txtminute;
55             }
56         }
57 
58         private void txtyear_leave(object sender, eventargs e)
59         {
60             if (txtyear.text.toint() < 1990)
61             {
62                 txtyear.text = currenttime.year.tostring();
63             }
64             currenttime = (txtyear.text + currenttime.tostring("-mm-dd hh:mm:ss")).todate();
65         }
66 
67         private void txtmonth_leave(object sender, eventargs e)
68         {
69             if (txtmonth.text.toint() < 1)
70             {
71                 txtmonth.text = currenttime.month.tostring().padleft(2, '0');
72             }
73             txtmonth.text = txtmonth.text.padleft(2, '0');
74             currenttime = (currenttime.tostring("yyyy-" + txtmonth.text + "-dd hh:mm:ss")).todate();
75         }
76 
77         private void txtday_leave(object sender, eventargs e)
78         {
79             if (txtday.text.toint() < 1 || txtday.text.toint() > datetime.daysinmonth(currenttime.year, currenttime.month))
80             {
81                 txtday.text = currenttime.day.tostring().padleft(2, '0');
82             }
83             txtday.text = txtday.text.padleft(2, '0');
84             currenttime = (currenttime.tostring("yyyy-mm-" + txtday.text + " hh:mm:ss")).todate();
85         }
86 
87         private void txthour_leave(object sender, eventargs e)
88         {
89             if (txthour.text.toint() < 1)
90             {
91                 txthour.text = currenttime.hour.tostring().padleft(2, '0');
92             }
93             txthour.text = txthour.text.padleft(2, '0');
94             currenttime = (currenttime.tostring("yyyy-mm-dd " + txthour.text + ":mm:ss")).todate();
95         }
96 
97         private void txtminute_leave(object sender, eventargs e)
98         {
99             if (txtminute.text.toint() < 1)
100             {
101                 txtminute.text = currenttime.minute.tostring().padleft(2, '0');
102             }
103             txtminute.text = txtminute.text.padleft(2, '0');
104             currenttime = (currenttime.tostring("yyyy-mm-dd hh:" + txtminute.text + ":ss")).todate();
105         }
106 
107         private void txt_sizechanged(object sender, eventargs e)
108         {
109             panel1.height = (sender as textboxex).height;
110         }

完整代码

  1 // 版权所有  黄正辉  交流群:568015492   qq:623128629
2 // 文件名称:ucdatepickerext.cs
3 // 创建日期:2019-08-15 15:59:46
4 // 功能描述:datetime
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 
16 namespace hzh_controls.controls
17 {
18     public partial class ucdatepickerext : uccontrolbase
19     {
20         forms.frmanchor m_frmanchor;
21         ucdatetimeselectpan m_selectpan = null;
22         datetimepickertype m_type = datetimepickertype.datetime;
23         [description("时间类型"), category("自定义")]
24         public datetimepickertype timetype
25         {
26             get { return m_type; }
27             set
28             {
29                 m_type = value;
30                 if (value == datetimepickertype.datetime)
31                 {
32                     txtyear.visible = true;
33                     label1.visible = true;
34                     txtmonth.visible = true;
35                     label2.visible = true;
36                     txtday.visible = true;
37                     label3.visible = true;
38                     txthour.visible = true;
39                     label4.visible = true;
40                     txtminute.visible = true;
41                     label5.visible = true;
42                 }
43                 else if (value == datetimepickertype.date)
44                 {
45                     txtyear.visible = true;
46                     label1.visible = true;
47                     txtmonth.visible = true;
48                     label2.visible = true;
49                     txtday.visible = true;
50                     label3.visible = true;
51                     txthour.visible = false;
52                     label4.visible = false;
53                     txtminute.visible = false;
54                     label5.visible = false;
55                 }
56                 else
57                 {
58                     txtyear.visible = false;
59                     label1.visible = false;
60                     txtmonth.visible = false;
61                     label2.visible = false;
62                     txtday.visible = false;
63                     label3.visible = false;
64                     txthour.visible = true;
65                     label4.visible = true;
66                     txtminute.visible = true;
67                     label5.visible = true;
68                 }
69             }
70         }
71 
72         private datetime currenttime = datetime.now;
73 
74         private int timefontsize = 20;
75         [description("时间字体大小"), category("自定义")]
76         public int timefontsize
77         {
78             get { return timefontsize; }
79             set
80             {
81                 if (timefontsize != value)
82                 {
83                     timefontsize = value;
84                     foreach (control c in panel1.controls)
85                     {
86                         c.font = new font(c.font.name, value);
87                     }
88                 }
89             }
90         }
91 
92         [description("时间"), category("自定义")]
93         public datetime currenttime
94         {
95             get { return currenttime; }
96             set
97             {
98                 currenttime = value;
99                 settimetocontrol();
100             }
101         }
102 
103         private void settimetocontrol()
104         {
105             this.txtyear.text = currenttime.year.tostring();
106             this.txtmonth.text = currenttime.month.tostring().padleft(2, '0');
107             this.txtday.text = currenttime.day.tostring().padleft(2, '0');
108             this.txthour.text = currenttime.hour.tostring().padleft(2, '0');
109             this.txtminute.text = currenttime.minute.tostring().padleft(2, '0');
110         }
111         public ucdatepickerext()
112         {
113             initializecomponent();
114         }
115 
116         private void ucdatepickerext_load(object sender, eventargs e)
117         {
118             settimetocontrol();
119             panel1.height = this.txtday.height;
120             panel1.height = this.txthour.height;
121             setevent(this);
122         }
123 
124         private void setevent(control c)
125         {
126             if (c != null)
127             {
128                 c.mousedown += c_mousedown;
129                 foreach (control item in c.controls)
130                 {
131                     setevent(item);
132                 }
133             }
134         }
135 
136         void c_mousedown(object sender, mouseeventargs e)
137         {
138             if (m_selectpan == null)
139             {
140                 m_selectpan = new ucdatetimeselectpan();
141                 m_selectpan.selectedtimeevent += uc_selectedtimeevent;
142                 m_selectpan.canceltimeevent += m_selectpan_canceltimeevent;
143             }
144             m_selectpan.currenttime = currenttime;
145             m_selectpan.timetype = m_type;
146             m_frmanchor = new forms.frmanchor(this, m_selectpan);
147             m_frmanchor.show(this.findform());
148         }
149 
150         void m_selectpan_canceltimeevent(object sender, eventargs e)
151         {
152             m_frmanchor.hide();
153         }
154 
155         void uc_selectedtimeevent(object sender, eventargs e)
156         {
157             currenttime = m_selectpan.currenttime;
158             m_frmanchor.hide();
159         }
160 
161         private void txtyear_textchanged(object sender, eventargs e)
162         {
163             if (txtyear.text.length == 4)
164                 this.activecontrol = txtmonth;
165         }
166 
167         private void txtmonth_textchanged(object sender, eventargs e)
168         {
169             if (txtmonth.text.length == 2 || txtmonth.text.toint() >= 3)
170             {
171                 this.activecontrol = txtday;
172             }
173         }
174 
175         private void txtday_textchanged(object sender, eventargs e)
176         {
177             if (m_type == datetimepickertype.date)
178                 return;
179             if (txtday.text.length == 2 || txtday.text.toint() >= 4)
180             {
181                 this.activecontrol = txthour;
182             }
183         }
184 
185         private void txthour_textchanged(object sender, eventargs e)
186         {
187             if (txthour.text.length == 2 || txthour.text.toint() >= 3)
188             {
189                 this.activecontrol = txtminute;
190             }
191         }
192 
193         private void txtyear_leave(object sender, eventargs e)
194         {
195             if (txtyear.text.toint() < 1990)
196             {
197                 txtyear.text = currenttime.year.tostring();
198             }
199             currenttime = (txtyear.text + currenttime.tostring("-mm-dd hh:mm:ss")).todate();
200         }
201 
202         private void txtmonth_leave(object sender, eventargs e)
203         {
204             if (txtmonth.text.toint() < 1)
205             {
206                 txtmonth.text = currenttime.month.tostring().padleft(2, '0');
207             }
208             txtmonth.text = txtmonth.text.padleft(2, '0');
209             currenttime = (currenttime.tostring("yyyy-" + txtmonth.text + "-dd hh:mm:ss")).todate();
210         }
211 
212         private void txtday_leave(object sender, eventargs e)
213         {
214             if (txtday.text.toint() < 1 || txtday.text.toint() > datetime.daysinmonth(currenttime.year, currenttime.month))
215             {
216                 txtday.text = currenttime.day.tostring().padleft(2, '0');
217             }
218             txtday.text = txtday.text.padleft(2, '0');
219             currenttime = (currenttime.tostring("yyyy-mm-" + txtday.text + " hh:mm:ss")).todate();
220         }
221 
222         private void txthour_leave(object sender, eventargs e)
223         {
224             if (txthour.text.toint() < 1)
225             {
226                 txthour.text = currenttime.hour.tostring().padleft(2, '0');
227             }
228             txthour.text = txthour.text.padleft(2, '0');
229             currenttime = (currenttime.tostring("yyyy-mm-dd " + txthour.text + ":mm:ss")).todate();
230         }
231 
232         private void txtminute_leave(object sender, eventargs e)
233         {
234             if (txtminute.text.toint() < 1)
235             {
236                 txtminute.text = currenttime.minute.tostring().padleft(2, '0');
237             }
238             txtminute.text = txtminute.text.padleft(2, '0');
239             currenttime = (currenttime.tostring("yyyy-mm-dd hh:" + txtminute.text + ":ss")).todate();
240         }
241 
242         private void txt_sizechanged(object sender, eventargs e)
243         {
244             panel1.height = (sender as textboxex).height;
245         }
246     }
247 }
  1 namespace hzh_controls.controls
2 {
3     partial class ucdatepickerext
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.panel1 = new system.windows.forms.panel();
32             this.txtminute = new hzh_controls.controls.textboxex();
33             this.label4 = new system.windows.forms.label();
34             this.txthour = new hzh_controls.controls.textboxex();
35             this.label3 = new system.windows.forms.label();
36             this.txtday = new hzh_controls.controls.textboxex();
37             this.label2 = new system.windows.forms.label();
38             this.txtmonth = new hzh_controls.controls.textboxex();
39             this.label1 = new system.windows.forms.label();
40             this.txtyear = new hzh_controls.controls.textboxex();
41             this.label5 = new system.windows.forms.label();
42             this.panel1.suspendlayout();
43             this.suspendlayout();
44             // 
45             // panel1
46             // 
47             this.panel1.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.left | system.windows.forms.anchorstyles.right)));
48             this.panel1.controls.add(this.label5);
49             this.panel1.controls.add(this.txtminute);
50             this.panel1.controls.add(this.label4);
51             this.panel1.controls.add(this.txthour);
52             this.panel1.controls.add(this.label3);
53             this.panel1.controls.add(this.txtday);
54             this.panel1.controls.add(this.label2);
55             this.panel1.controls.add(this.txtmonth);
56             this.panel1.controls.add(this.label1);
57             this.panel1.controls.add(this.txtyear);
58             this.panel1.location = new system.drawing.point(3, 6);
59             this.panel1.maximumsize = new system.drawing.size(0, 27);
60             this.panel1.name = "panel1";
61             this.panel1.size = new system.drawing.size(339, 27);
62             this.panel1.tabindex = 9;
63             // 
64             // txtminute
65             // 
66             this.txtminute.borderstyle = system.windows.forms.borderstyle.none;
67             this.txtminute.declength = 2;
68             this.txtminute.dock = system.windows.forms.dockstyle.left;
69             this.txtminute.font = new system.drawing.font("arial unicode ms", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
70             this.txtminute.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
71             this.txtminute.inputtype = textinputtype.integer;
72             this.txtminute.location = new system.drawing.point(272, 0);
73             this.txtminute.maxvalue = new decimal(new int[] {
74             59,
75             0,
76             0,
77             0});
78             this.txtminute.minvalue = new decimal(new int[] {
79             0,
80             0,
81             0,
82             0});
83             this.txtminute.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
84             this.txtminute.name = "txtminute";
85             this.txtminute.oldtext = null;
86             this.txtminute.promptcolor = system.drawing.color.gray;
87             this.txtminute.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
88             this.txtminute.prompttext = "";
89             this.txtminute.regexpattern = "";
90             this.txtminute.size = new system.drawing.size(29, 27);
91             this.txtminute.tabindex = 5;
92             this.txtminute.text = "59";
93             this.txtminute.textalign = system.windows.forms.horizontalalignment.center;
94             this.txtminute.sizechanged += new system.eventhandler(this.txt_sizechanged);
95             this.txtminute.leave += new system.eventhandler(this.txtminute_leave);
96             // 
97             // label4
98             // 
99             this.label4.autosize = true;
100             this.label4.dock = system.windows.forms.dockstyle.left;
101             this.label4.font = new system.drawing.font("微软雅黑", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
102             this.label4.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
103             this.label4.location = new system.drawing.point(240, 0);
104             this.label4.name = "label4";
105             this.label4.size = new system.drawing.size(32, 27);
106             this.label4.tabindex = 16;
107             this.label4.text = "时";
108             this.label4.textalign = system.drawing.contentalignment.middleleft;
109             // 
110             // txthour
111             // 
112             this.txthour.borderstyle = system.windows.forms.borderstyle.none;
113             this.txthour.declength = 2;
114             this.txthour.dock = system.windows.forms.dockstyle.left;
115             this.txthour.font = new system.drawing.font("arial unicode ms", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
116             this.txthour.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
117             this.txthour.inputtype = textinputtype.integer;
118             this.txthour.location = new system.drawing.point(211, 0);
119             this.txthour.maxvalue = new decimal(new int[] {
120             23,
121             0,
122             0,
123             0});
124             this.txthour.minvalue = new decimal(new int[] {
125             0,
126             0,
127             0,
128             0});
129             this.txthour.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
130             this.txthour.name = "txthour";
131             this.txthour.oldtext = null;
132             this.txthour.promptcolor = system.drawing.color.gray;
133             this.txthour.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
134             this.txthour.prompttext = "";
135             this.txthour.regexpattern = "";
136             this.txthour.size = new system.drawing.size(29, 27);
137             this.txthour.tabindex = 4;
138             this.txthour.text = "23";
139             this.txthour.textalign = system.windows.forms.horizontalalignment.center;
140             this.txthour.sizechanged += new system.eventhandler(this.txt_sizechanged);
141             this.txthour.textchanged += new system.eventhandler(this.txthour_textchanged);
142             this.txthour.leave += new system.eventhandler(this.txthour_leave);
143             // 
144             // label3
145             // 
146             this.label3.autosize = true;
147             this.label3.dock = system.windows.forms.dockstyle.left;
148             this.label3.font = new system.drawing.font("微软雅黑", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
149             this.label3.location = new system.drawing.point(173, 0);
150             this.label3.margin = new system.windows.forms.padding(0);
151             this.label3.name = "label3";
152             this.label3.size = new system.drawing.size(38, 27);
153             this.label3.tabindex = 14;
154             this.label3.text = " 日";
155             this.label3.textalign = system.drawing.contentalignment.middleleft;
156             // 
157             // txtday
158             // 
159             this.txtday.borderstyle = system.windows.forms.borderstyle.none;
160             this.txtday.declength = 2;
161             this.txtday.dock = system.windows.forms.dockstyle.left;
162             this.txtday.font = new system.drawing.font("arial unicode ms", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
163             this.txtday.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
164             this.txtday.inputtype = textinputtype.integer;
165             this.txtday.location = new system.drawing.point(144, 0);
166             this.txtday.maxvalue = new decimal(new int[] {
167             31,
168             0,
169             0,
170             0});
171             this.txtday.minvalue = new decimal(new int[] {
172             0,
173             0,
174             0,
175             0});
176             this.txtday.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
177             this.txtday.name = "txtday";
178             this.txtday.oldtext = null;
179             this.txtday.promptcolor = system.drawing.color.gray;
180             this.txtday.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
181             this.txtday.prompttext = "";
182             this.txtday.regexpattern = "";
183             this.txtday.size = new system.drawing.size(29, 27);
184             this.txtday.tabindex = 3;
185             this.txtday.text = "12";
186             this.txtday.textalign = system.windows.forms.horizontalalignment.center;
187             this.txtday.sizechanged += new system.eventhandler(this.txt_sizechanged);
188             this.txtday.textchanged += new system.eventhandler(this.txtday_textchanged);
189             this.txtday.leave += new system.eventhandler(this.txtday_leave);
190             // 
191             // label2
192             // 
193             this.label2.autosize = true;
194             this.label2.dock = system.windows.forms.dockstyle.left;
195             this.label2.font = new system.drawing.font("微软雅黑", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
196             this.label2.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
197             this.label2.location = new system.drawing.point(112, 0);
198             this.label2.name = "label2";
199             this.label2.size = new system.drawing.size(32, 27);
200             this.label2.tabindex = 12;
201             this.label2.text = "月";
202             this.label2.textalign = system.drawing.contentalignment.middleleft;
203             // 
204             // txtmonth
205             // 
206             this.txtmonth.borderstyle = system.windows.forms.borderstyle.none;
207             this.txtmonth.declength = 2;
208             this.txtmonth.dock = system.windows.forms.dockstyle.left;
209             this.txtmonth.font = new system.drawing.font("arial unicode ms", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
210             this.txtmonth.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
211             this.txtmonth.inputtype = textinputtype.integer;
212             this.txtmonth.location = new system.drawing.point(83, 0);
213             this.txtmonth.maxvalue = new decimal(new int[] {
214             12,
215             0,
216             0,
217             0});
218             this.txtmonth.minvalue = new decimal(new int[] {
219             0,
220             0,
221             0,
222             0});
223             this.txtmonth.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
224             this.txtmonth.name = "txtmonth";
225             this.txtmonth.oldtext = null;
226             this.txtmonth.promptcolor = system.drawing.color.gray;
227             this.txtmonth.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
228             this.txtmonth.prompttext = "";
229             this.txtmonth.regexpattern = "";
230             this.txtmonth.size = new system.drawing.size(29, 27);
231             this.txtmonth.tabindex = 2;
232             this.txtmonth.text = "12";
233             this.txtmonth.textalign = system.windows.forms.horizontalalignment.center;
234             this.txtmonth.sizechanged += new system.eventhandler(this.txt_sizechanged);
235             this.txtmonth.textchanged += new system.eventhandler(this.txtmonth_textchanged);
236             this.txtmonth.leave += new system.eventhandler(this.txtmonth_leave);
237             // 
238             // label1
239             // 
240             this.label1.autosize = true;
241             this.label1.dock = system.windows.forms.dockstyle.left;
242             this.label1.font = new system.drawing.font("微软雅黑", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
243             this.label1.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
244             this.label1.location = new system.drawing.point(51, 0);
245             this.label1.margin = new system.windows.forms.padding(0);
246             this.label1.name = "label1";
247             this.label1.size = new system.drawing.size(32, 27);
248             this.label1.tabindex = 10;
249             this.label1.text = "年";
250             this.label1.textalign = system.drawing.contentalignment.middleleft;
251             // 
252             // txtyear
253             // 
254             this.txtyear.borderstyle = system.windows.forms.borderstyle.none;
255             this.txtyear.declength = 2;
256             this.txtyear.dock = system.windows.forms.dockstyle.left;
257             this.txtyear.font = new system.drawing.font("arial unicode ms", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
258             this.txtyear.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
259             this.txtyear.inputtype = textinputtype.integer;
260             this.txtyear.location = new system.drawing.point(0, 0);
261             this.txtyear.maxvalue = new decimal(new int[] {
262             2099,
263             0,
264             0,
265             0});
266             this.txtyear.minvalue = new decimal(new int[] {
267             0,
268             0,
269             0,
270             0});
271             this.txtyear.myrectangle = new system.drawing.rectangle(0, 0, 0, 0);
272             this.txtyear.name = "txtyear";
273             this.txtyear.oldtext = null;
274             this.txtyear.promptcolor = system.drawing.color.gray;
275             this.txtyear.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
276             this.txtyear.prompttext = "";
277             this.txtyear.regexpattern = "";
278             this.txtyear.size = new system.drawing.size(51, 27);
279             this.txtyear.tabindex = 1;
280             this.txtyear.text = "2019";
281             this.txtyear.textalign = system.windows.forms.horizontalalignment.center;
282             this.txtyear.sizechanged += new system.eventhandler(this.txt_sizechanged);
283             this.txtyear.textchanged += new system.eventhandler(this.txtyear_textchanged);
284             this.txtyear.leave += new system.eventhandler(this.txtyear_leave);
285             // 
286             // label5
287             // 
288             this.label5.autosize = true;
289             this.label5.dock = system.windows.forms.dockstyle.left;
290             this.label5.font = new system.drawing.font("微软雅黑", 20f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
291             this.label5.forecolor = system.drawing.color.fromargb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
292             this.label5.location = new system.drawing.point(301, 0);
293             this.label5.name = "label5";
294             this.label5.size = new system.drawing.size(32, 27);
295             this.label5.tabindex = 17;
296             this.label5.text = "分";
297             this.label5.textalign = system.drawing.contentalignment.middleleft;
298             // 
299             // ucdatepickerext
300             // 
301             this.autoscalemode = system.windows.forms.autoscalemode.none;
302             this.backcolor = system.drawing.color.transparent;
303             this.conerradius = 5;
304             this.controls.add(this.panel1);
305             this.isshowrect = true;
306             this.isradius = true;
307             this.name = "ucdatepickerext";
308             this.padding = new system.windows.forms.padding(0, 10, 0, 0);
309             this.size = new system.drawing.size(345, 39);
310             this.load += new system.eventhandler(this.ucdatepickerext_load);
311             this.panel1.resumelayout(false);
312             this.panel1.performlayout();
313             this.resumelayout(false);
314 
315         }
316 
317         #endregion
318 
319         private system.windows.forms.panel panel1;
320         private system.windows.forms.label label4;
321         private system.windows.forms.label label3;
322         private system.windows.forms.label label2;
323         private system.windows.forms.label label1;
324         private textboxex txtminute;
325         private textboxex txthour;
326         private textboxex txtday;
327         private textboxex txtmonth;
328         private textboxex txtyear;
329         private system.windows.forms.label label5;
330     }
331 }

 

用处及效果

 

 

最后的话

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