(三十三)c#Winform自定义控件-日期控件

前提

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

开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control

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

欢迎前来交流探讨: 企鹅群568015492 (三十三)c#Winform自定义控件-日期控件

目录

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

准备工作

日期控件将分为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         }

全部代码

(三十三)c#Winform自定义控件-日期控件
  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 }
View Code
(三十三)c#Winform自定义控件-日期控件
  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 }
View Code

 

下面是时间选择面板

添加用户控件,命名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         }

完整代码

(三十三)c#Winform自定义控件-日期控件
  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 }
View Code
(三十三)c#Winform自定义控件-日期控件
  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 }
View Code

 

日期输入控件

添加一个用户控件,命名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         }

完整代码

(三十三)c#Winform自定义控件-日期控件
  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 }
View Code
(三十三)c#Winform自定义控件-日期控件
  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 }
View Code

 

用处及效果

(三十三)c#Winform自定义控件-日期控件

(三十三)c#Winform自定义控件-日期控件

 

 

最后的话

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

(三十三)c#Winform自定义控件-日期控件

上一篇:GDI不规则圆弧菜单控件----------WinForm控件开发系列


下一篇:delphi中nil、null、UnAssigned区别