前提

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

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

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

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

目录

https://blog.csdn.net/kwwwvagaa/article/details/100586547

准备工作

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

复制代码

全部代码

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCTimePanel.cs
// 创建日期:2019-08-15 15:59:56
// 功能描述:DateTime
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace HZH_Controls.Controls
{
    [ToolboxItem(false)]
    public partial class UCTimePanel : UserControl
    {
        public event EventHandler SelectSourceEvent;
        private List<KeyValuePair<string, string>> source = null;
        public bool FirstEvent { get; set; }

        public List<KeyValuePair<string, string>> Source
        {
            get { return source; }
            set
            {
                source = value;
                SetSource(value);
            }
        }

        private bool _IsShowBorder = false;

        public bool IsShowBorder
        {
            get { return _IsShowBorder; }
            set
            {
                _IsShowBorder = value;
                ucSplitLine_H1.Visible = value;
                ucSplitLine_H2.Visible = value;
                ucSplitLine_V1.Visible = value;
                ucSplitLine_V2.Visible = value;
            }
        }

        UCBtnExt selectBtn;
        /// <summary>
        /// 选中按钮
        /// </summary>
        public UCBtnExt SelectBtn
        {
            get { return selectBtn; }
            set
            {
                if (selectBtn != null && !selectBtn.IsDisposed)
                {
                    selectBtn.FillColor = System.Drawing.Color.White;
                    selectBtn.RectColor = System.Drawing.Color.White;
                    selectBtn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
                }
                bool blnEvent = FirstEvent ? true : (selectBtn != null);
                selectBtn = value;
                if (value != null)
                {
                    selectBtn.FillColor = System.Drawing.Color.FromArgb(255, 77, 59);
                    selectBtn.RectColor = System.Drawing.Color.FromArgb(255, 77, 59);
                    selectBtn.BtnForeColor = System.Drawing.Color.White;
                    if (blnEvent && SelectSourceEvent != null)
                        SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
                }
            }
        }
        public UCTimePanel()
        {
            InitializeComponent();
            this.SizeChanged += UCTimePanel_SizeChanged;
        }

        void UCTimePanel_SizeChanged(object sender, EventArgs e)
        {

        }

        private int row = 0;

        public int Row
        {
            get { return row; }
            set
            {
                row = value;
                ReloadPanel();
            }
        }


        private int column = 0;

        public int Column
        {
            get { return column; }
            set
            {
                column = value;
                ReloadPanel();
            }
        }

        private void UCTimePanel_Load(object sender, EventArgs e)
        {

        }

        #region 设置面板数据源
        /// <summary>
        /// 功能描述:设置面板数据源
        /// 作  者:HZH
        /// 创建日期:2019-06-25 15:02:15
        /// 任务编号:POS
        /// </summary>
        /// <param name="lstSource">lstSource</param>
        public void SetSource(List<KeyValuePair<string, string>> lstSource)
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                if (row <= 0 || column <= 0)
                    return;
                if (Source != lstSource)
                    Source = lstSource;
                int index = 0;
                SelectBtn = null;
                foreach (UCBtnExt btn in this.panMain.Controls)
                {
                    if (lstSource != null && index < lstSource.Count)
                    {
                        btn.BtnText = lstSource[index].Value;
                        btn.Tag = lstSource[index].Key;
                        index++;
                    }
                    else
                    {
                        btn.BtnText = "";
                        btn.Tag = null;
                    }
                }
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }
        #endregion
        /// <summary>
        /// 设置选中项
        /// </summary>
        /// <param name="strKey"></param>
        public void SetSelect(string strKey)
        {
            foreach (UCBtnExt item in this.panMain.Controls)
            {
                if (item.Tag != null && item.Tag.ToStringExt() == strKey)
                {
                    SelectBtn = item;
                    return;
                }
            }
            SelectBtn = new UCBtnExt();
        }

        #region 重置面板
        /// <summary>
        /// 功能描述:重置面板
        /// 作  者:HZH
        /// 创建日期:2019-06-25 15:02:05
        /// 任务编号:POS
        /// </summary>
        public void ReloadPanel()
        {
            if (row <= 0 || column <= 0)
                return;
            SelectBtn = null;
            this.panMain.Controls.Clear();
            this.panMain.ColumnCount = column;
            this.panMain.ColumnStyles.Clear();
            for (int i = 0; i < column; i++)
            {
                this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            }

            this.panMain.RowCount = row;
            this.panMain.RowStyles.Clear();
            for (int i = 0; i < row; i++)
            {
                this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            }

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    UCBtnExt btn = new UCBtnExt();
                    btn.BackColor = System.Drawing.Color.Transparent;
                    btn.BtnBackColor = System.Drawing.Color.Transparent;
                    btn.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
                    btn.BtnForeColor = System.Drawing.Color.FromArgb(66, 66, 66);
                    btn.ConerRadius = 5;
                    btn.Dock = DockStyle.Fill;
                    btn.FillColor = System.Drawing.Color.White;
                    btn.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
                    btn.Cursor = Cursor.Current;
                    btn.IsShowRect = true;
                    btn.IsRadius = true;
                    btn.IsShowTips = false;
                    btn.Name = "btn_" + i + "_" + j;
                    btn.RectColor = System.Drawing.Color.White;
                    btn.RectWidth = 1;
                    btn.Width = this.Width;
                    btn.TabIndex = 0;
                    btn.TipsText = "";
                    btn.BtnClick += btn_BtnClick;
                    this.panMain.Controls.Add(btn, j, i);
                }
            }

            if (Source != null)
            {
                SetSource(Source);
            }
        }
        #endregion

        void btn_BtnClick(object sender, EventArgs e)
        {
            var btn = (UCBtnExt)sender;
            if (btn.Tag == null)
                return;
            SelectBtn = btn;
        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCTimePanel
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panMain = new System.Windows.Forms.TableLayoutPanel();
            this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
            this.ucSplitLine_V2 = new HZH_Controls.Controls.UCSplitLine_V();
            this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
            this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
            this.SuspendLayout();
            // 
            // panMain
            // 
            this.panMain.ColumnCount = 1;
            this.panMain.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panMain.Location = new System.Drawing.Point(1, 1);
            this.panMain.Name = "panMain";
            this.panMain.RowCount = 1;
            this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
            this.panMain.Size = new System.Drawing.Size(99, 228);
            this.panMain.TabIndex = 0;
            // 
            // ucSplitLine_V1
            // 
            this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
            this.ucSplitLine_V1.Location = new System.Drawing.Point(0, 0);
            this.ucSplitLine_V1.Name = "ucSplitLine_V1";
            this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 230);
            this.ucSplitLine_V1.TabIndex = 0;
            this.ucSplitLine_V1.TabStop = false;
            this.ucSplitLine_V1.Visible = false;
            // 
            // ucSplitLine_V2
            // 
            this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right;
            this.ucSplitLine_V2.Location = new System.Drawing.Point(100, 0);
            this.ucSplitLine_V2.Name = "ucSplitLine_V2";
            this.ucSplitLine_V2.Size = new System.Drawing.Size(1, 230);
            this.ucSplitLine_V2.TabIndex = 1;
            this.ucSplitLine_V2.TabStop = false;
            this.ucSplitLine_V2.Visible = false;
            // 
            // ucSplitLine_H1
            // 
            this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
            this.ucSplitLine_H1.Location = new System.Drawing.Point(1, 0);
            this.ucSplitLine_H1.Name = "ucSplitLine_H1";
            this.ucSplitLine_H1.Size = new System.Drawing.Size(99, 1);
            this.ucSplitLine_H1.TabIndex = 0;
            this.ucSplitLine_H1.TabStop = false;
            this.ucSplitLine_H1.Visible = false;
            // 
            // ucSplitLine_H2
            // 
            this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.ucSplitLine_H2.Location = new System.Drawing.Point(1, 229);
            this.ucSplitLine_H2.Name = "ucSplitLine_H2";
            this.ucSplitLine_H2.Size = new System.Drawing.Size(99, 1);
            this.ucSplitLine_H2.TabIndex = 2;
            this.ucSplitLine_H2.TabStop = false;
            this.ucSplitLine_H2.Visible = false;
            // 
            // UCTimePanel
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.panMain);
            this.Controls.Add(this.ucSplitLine_H2);
            this.Controls.Add(this.ucSplitLine_H1);
            this.Controls.Add(this.ucSplitLine_V2);
            this.Controls.Add(this.ucSplitLine_V1);
            this.Name = "UCTimePanel";
            this.Size = new System.Drawing.Size(101, 230);
            this.Load += new System.EventHandler(this.UCTimePanel_Load);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.TableLayoutPanel panMain;
        private UCSplitLine_V ucSplitLine_V1;
        private UCSplitLine_V ucSplitLine_V2;
        private UCSplitLine_H ucSplitLine_H1;
        private UCSplitLine_H ucSplitLine_H2;
    }
}

下面是时间选择面板

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

复制代码

完整代码

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCDateTimeSelectPan.cs
// 创建日期:2019-08-15 15:59:51
// 功能描述:DateTime
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace HZH_Controls.Controls
{
    [ToolboxItem(false)]
    public partial class UCDateTimeSelectPan : UserControl
    {
        [Description("确定事件"), Category("自定义")]
        public event EventHandler SelectedTimeEvent;
        [Description("取消事件"), Category("自定义")]
        public event EventHandler CancelTimeEvent;
        private bool autoSelectNext = true;
        [Description("自动选中下一级"), Category("自定义")]
        public bool AutoSelectNext
        {
            get { return autoSelectNext; }
            set { autoSelectNext = value; }
        }

        DateTime m_dt = DateTime.Now;

        public DateTime CurrentTime
        {
            get { return m_dt; }
            set
            {
                m_dt = value;
                SetTimeToControl();
            }
        }
        UCBtnExt m_thisBtn = null;

        DateTimePickerType m_type = DateTimePickerType.DateTime;
        [Description("时间类型"), Category("自定义")]
        public DateTimePickerType TimeType
        {
            get { return m_type; }
            set { m_type = value; }
        }
        public UCDateTimeSelectPan()
        {
            InitializeComponent();
            panTime.SelectSourceEvent += panTime_SelectSourceEvent;
            this.TabStop = false;
        }
        public UCDateTimeSelectPan(DateTime dt)
        {
            m_dt = dt;
            InitializeComponent();
            panTime.SelectSourceEvent += panTime_SelectSourceEvent;
            this.TabStop = false;
        }

        void panTime_SelectSourceEvent(object sender, EventArgs e)
        {
            string strKey = sender.ToString();
            if (m_thisBtn == btnYear)
            {
                m_dt = (strKey + "-" + m_dt.Month + "-" + m_dt.Day + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
            }
            else if (m_thisBtn == btnMonth)
            {
                m_dt = (m_dt.Year + "-" + strKey + "-" + m_dt.Day + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
            }
            else if (m_thisBtn == btnDay)
            {
                m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + strKey + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
            }
            else if (m_thisBtn == btnHour)
            {
                m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + m_dt.Day + " " + strKey + ":" + m_dt.Minute).ToDate();
            }
            else if (m_thisBtn == btnMinute)
            {
                m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + m_dt.Day + " " + m_dt.Hour + ":" + strKey).ToDate();
            }
            SetTimeToControl();
            if (this.Visible)
            {
                if (autoSelectNext)
                {
                    if (m_thisBtn == btnYear)
                    {
                        SetSelectType(btnMonth);
                    }
                    else if (m_thisBtn == btnMonth)
                    {
                        SetSelectType(btnDay);
                    }
                    else if (m_thisBtn == btnDay)
                    {
                        if (m_type == DateTimePickerType.DateTime || m_type == DateTimePickerType.Time)
                            SetSelectType(btnHour);
                    }
                    else if (m_thisBtn == btnHour)
                    {
                        SetSelectType(btnMinute);
                    }
                }
            }
        }

        private void UCDateTimePickerExt_Load(object sender, EventArgs e)
        {
            SetTimeToControl();

            if (m_type == DateTimePickerType.Date)
            {
                btnHour.Visible = false;
                btnMinute.Visible = false;
            }
            else if (m_type == DateTimePickerType.Time)
            {
                btnYear.Visible = false;
                btnMonth.Visible = false;
                btnDay.Visible = false;
                sp1.Visible = false;
                sp2.Visible = false;
                sp3.Visible = false;
            }
            if ((int)m_type <= 2)
            {
                SetSelectType(btnYear);
            }
            else
            {
                SetSelectType(btnHour);
            }
        }

        private void SetTimeToControl()
        {
            btnYear.Tag = m_dt.Year;
            btnYear.BtnText = m_dt.Year + "年";
            btnMonth.Tag = m_dt.Month;
            btnMonth.BtnText = m_dt.Month.ToString().PadLeft(2, '0') + "月";
            btnDay.Tag = m_dt.Day;
            btnDay.BtnText = m_dt.Day.ToString().PadLeft(2, '0') + "日";
            btnHour.Tag = m_dt.Hour;
            btnHour.BtnText = m_dt.Hour.ToString().PadLeft(2, '0') + "时";
            btnMinute.Tag = m_dt.Minute;
            btnMinute.BtnText = m_dt.Minute.ToString().PadLeft(2, '0') + "分";
        }

        private void SetSelectType(UCBtnExt btn)
        {
            try
            {
                ControlHelper.FreezeControl(this, true);
                if (m_thisBtn != null)
                {
                    m_thisBtn.FillColor = Color.White;
                    m_thisBtn.BtnForeColor = Color.FromArgb(255, 77, 59);
                }
                m_thisBtn = btn;
                if (m_thisBtn != null)
                {
                    m_thisBtn.FillColor = Color.FromArgb(255, 77, 59);
                    m_thisBtn.BtnForeColor = Color.White;

                    List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
                    panTime.SuspendLayout();

                    if (btn == btnYear)
                    {
                        panLeft.Visible = true;
                        panRight.Visible = true;
                    }
                    else
                    {
                        panLeft.Visible = false;
                        panRight.Visible = false;
                    }

                    if (btn == btnYear)
                    {
                        panTime.Row = 5;
                        panTime.Column = 6;
                        int intYear = m_dt.Year - m_dt.Year % 30;
                        for (int i = 0; i < 30; i++)
                        {
                            lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
                        }
                    }
                    else if (btn == btnMonth)
                    {
                        panTime.Row = 3;
                        panTime.Column = 4;
                        for (int i = 1; i <= 12; i++)
                        {
                            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")))));
                        }
                    }
                    else if (btn == btnDay)
                    {
                        panTime.Column = 7;
                        int intDayCount = DateTime.DaysInMonth(m_dt.Year, m_dt.Month);
                        int intIndex = (int)(m_dt.DayOfWeek);
                        panTime.Row = (intDayCount + intIndex) / 7 + ((intDayCount + intIndex) % 7 != 0 ? 1 : 0);
                        for (int i = 0; i < intIndex; i++)
                        {
                            lstSource.Add(new KeyValuePair<string, string>("", ""));
                        }
                        for (int i = 1; i <= intDayCount; i++)
                        {
                            lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(2, '0')));
                        }
                    }
                    else if (btn == btnHour)
                    {
                        panTime.Row = 4;
                        panTime.Column = 6;
                        for (int i = 0; i <= 24; i++)
                        {
                            lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString() + "时"));
                        }
                    }
                    else if (btn == btnMinute)
                    {
                        panTime.Row = 5;
                        panTime.Column = 12;
                        for (int i = 0; i <= 60; i++)
                        {
                            lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(2, '0')));
                        }
                    }
                    panTime.Source = lstSource;
                    panTime.SetSelect(btn.Tag.ToStringExt());
                    panTime.ResumeLayout(true);

                    // panTime.Enabled = true;
                }
            }
            finally
            {
                ControlHelper.FreezeControl(this, false);
            }
        }

        private void btnTime_BtnClick(object sender, EventArgs e)
        {
            SetSelectType((UCBtnExt)sender);
        }

        private void panLeft_MouseDown(object sender, MouseEventArgs e)
        {
            List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
            int intYear = this.panTime.Source[0].Key.ToInt() - this.panTime.Source[0].Key.ToInt() % 30 - 30;
            panTime.SuspendLayout();
            panTime.Row = 5;
            panTime.Column = 6;
            for (int i = 0; i < 30; i++)
            {
                lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
            }
            panTime.Source = lstSource;
            panTime.SetSelect(btnYear.Tag.ToStringExt());
            panTime.ResumeLayout(true);
        }

        private void panRight_MouseDown(object sender, MouseEventArgs e)
        {
            List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
            int intYear = this.panTime.Source[0].Key.ToInt() - this.panTime.Source[0].Key.ToInt() % 30 + 30;
            panTime.SuspendLayout();
            panTime.Row = 5;
            panTime.Column = 6;
            for (int i = 0; i < 30; i++)
            {
                lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
            }
            panTime.Source = lstSource;
            panTime.SetSelect(btnYear.Tag.ToStringExt());
            panTime.ResumeLayout(true);
        }

        private void btnOk_BtnClick(object sender, EventArgs e)
        {
            if (SelectedTimeEvent != null)
                SelectedTimeEvent(m_dt, null);
        }

        private void btnCancel_BtnClick(object sender, EventArgs e)
        {
            if (CancelTimeEvent != null)
            {
                CancelTimeEvent(null, null);
            }
        }
    }
}
namespace HZH_Controls.Controls
{
    public partial class UCDateTimeSelectPan
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnMinute = new HZH_Controls.Controls.UCBtnExt();
            this.sp4 = new System.Windows.Forms.Panel();
            this.btnHour = new HZH_Controls.Controls.UCBtnExt();
            this.sp3 = new System.Windows.Forms.Panel();
            this.btnDay = new HZH_Controls.Controls.UCBtnExt();
            this.sp2 = new System.Windows.Forms.Panel();
            this.btnMonth = new HZH_Controls.Controls.UCBtnExt();
            this.sp1 = new System.Windows.Forms.Panel();
            this.btnYear = new HZH_Controls.Controls.UCBtnExt();
            this.panel2 = new System.Windows.Forms.Panel();
            this.btnCancel = new HZH_Controls.Controls.UCBtnExt();
            this.btnOk = new HZH_Controls.Controls.UCBtnExt();
            this.panMian = new System.Windows.Forms.Panel();
            this.panTime = new HZH_Controls.Controls.UCTimePanel();
            this.panRight = new System.Windows.Forms.Panel();
            this.panLeft = new System.Windows.Forms.Panel();
            this.ucSplitLine_H2 = new HZH_Controls.Controls.UCSplitLine_H();
            this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panMian.SuspendLayout();
            this.panel3.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnMinute);
            this.panel1.Controls.Add(this.sp4);
            this.panel1.Controls.Add(this.btnHour);
            this.panel1.Controls.Add(this.sp3);
            this.panel1.Controls.Add(this.btnDay);
            this.panel1.Controls.Add(this.sp2);
            this.panel1.Controls.Add(this.btnMonth);
            this.panel1.Controls.Add(this.sp1);
            this.panel1.Controls.Add(this.btnYear);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(9, 9);
            this.panel1.Name = "panel1";
            this.panel1.Padding = new System.Windows.Forms.Padding(10, 7, 10, 7);
            this.panel1.Size = new System.Drawing.Size(497, 45);
            this.panel1.TabIndex = 0;
            // 
            // btnMinute
            // 
            this.btnMinute.BackColor = System.Drawing.Color.Transparent;
            this.btnMinute.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnMinute.BtnFont = new System.Drawing.Font("微软雅黑", 12F);
            this.btnMinute.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnMinute.BtnText = "30分";
            this.btnMinute.ConerRadius = 5;
            this.btnMinute.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnMinute.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnMinute.FillColor = System.Drawing.Color.White;
            this.btnMinute.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnMinute.IsRadius = true;
            this.btnMinute.IsShowRect = true;
            this.btnMinute.IsShowTips = false;
            this.btnMinute.Location = new System.Drawing.Point(406, 7);
            this.btnMinute.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnMinute.Name = "btnMinute";
            this.btnMinute.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnMinute.RectWidth = 1;
            this.btnMinute.Size = new System.Drawing.Size(80, 31);
            this.btnMinute.TabIndex = 1;
            this.btnMinute.TabStop = false;
            this.btnMinute.TipsText = "";
            this.btnMinute.BtnClick += new System.EventHandler(this.btnTime_BtnClick);
            // 
            // sp4
            // 
            this.sp4.Dock = System.Windows.Forms.DockStyle.Left;
            this.sp4.Location = new System.Drawing.Point(387, 7);
            this.sp4.Name = "sp4";
            this.sp4.Size = new System.Drawing.Size(19, 31);
            this.sp4.TabIndex = 5;
            // 
            // btnHour
            // 
            this.btnHour.BackColor = System.Drawing.Color.Transparent;
            this.btnHour.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnHour.BtnFont = new System.Drawing.Font("微软雅黑", 12F);
            this.btnHour.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnHour.BtnText = "12时";
            this.btnHour.ConerRadius = 5;
            this.btnHour.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnHour.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnHour.FillColor = System.Drawing.Color.White;
            this.btnHour.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnHour.IsRadius = true;
            this.btnHour.IsShowRect = true;
            this.btnHour.IsShowTips = false;
            this.btnHour.Location = new System.Drawing.Point(307, 7);
            this.btnHour.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnHour.Name = "btnHour";
            this.btnHour.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnHour.RectWidth = 1;
            this.btnHour.Size = new System.Drawing.Size(80, 31);
            this.btnHour.TabIndex = 1;
            this.btnHour.TabStop = false;
            this.btnHour.TipsText = "";
            this.btnHour.BtnClick += new System.EventHandler(this.btnTime_BtnClick);
            // 
            // sp3
            // 
            this.sp3.Dock = System.Windows.Forms.DockStyle.Left;
            this.sp3.Location = new System.Drawing.Point(288, 7);
            this.sp3.Name = "sp3";
            this.sp3.Size = new System.Drawing.Size(19, 31);
            this.sp3.TabIndex = 4;
            // 
            // btnDay
            // 
            this.btnDay.BackColor = System.Drawing.Color.Transparent;
            this.btnDay.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnDay.BtnFont = new System.Drawing.Font("微软雅黑", 12F);
            this.btnDay.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnDay.BtnText = "30日";
            this.btnDay.ConerRadius = 5;
            this.btnDay.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnDay.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnDay.FillColor = System.Drawing.Color.White;
            this.btnDay.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnDay.IsRadius = true;
            this.btnDay.IsShowRect = true;
            this.btnDay.IsShowTips = false;
            this.btnDay.Location = new System.Drawing.Point(208, 7);
            this.btnDay.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnDay.Name = "btnDay";
            this.btnDay.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnDay.RectWidth = 1;
            this.btnDay.Size = new System.Drawing.Size(80, 31);
            this.btnDay.TabIndex = 1;
            this.btnDay.TabStop = false;
            this.btnDay.TipsText = "";
            this.btnDay.BtnClick += new System.EventHandler(this.btnTime_BtnClick);
            // 
            // sp2
            // 
            this.sp2.Dock = System.Windows.Forms.DockStyle.Left;
            this.sp2.Location = new System.Drawing.Point(189, 7);
            this.sp2.Name = "sp2";
            this.sp2.Size = new System.Drawing.Size(19, 31);
            this.sp2.TabIndex = 3;
            // 
            // btnMonth
            // 
            this.btnMonth.BackColor = System.Drawing.Color.Transparent;
            this.btnMonth.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnMonth.BtnFont = new System.Drawing.Font("微软雅黑", 12F);
            this.btnMonth.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnMonth.BtnText = "12月";
            this.btnMonth.ConerRadius = 5;
            this.btnMonth.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnMonth.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnMonth.FillColor = System.Drawing.Color.White;
            this.btnMonth.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnMonth.IsRadius = true;
            this.btnMonth.IsShowRect = true;
            this.btnMonth.IsShowTips = false;
            this.btnMonth.Location = new System.Drawing.Point(109, 7);
            this.btnMonth.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnMonth.Name = "btnMonth";
            this.btnMonth.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnMonth.RectWidth = 1;
            this.btnMonth.Size = new System.Drawing.Size(80, 31);
            this.btnMonth.TabIndex = 1;
            this.btnMonth.TabStop = false;
            this.btnMonth.TipsText = "";
            this.btnMonth.BtnClick += new System.EventHandler(this.btnTime_BtnClick);
            // 
            // sp1
            // 
            this.sp1.Dock = System.Windows.Forms.DockStyle.Left;
            this.sp1.Location = new System.Drawing.Point(90, 7);
            this.sp1.Name = "sp1";
            this.sp1.Size = new System.Drawing.Size(19, 31);
            this.sp1.TabIndex = 2;
            // 
            // btnYear
            // 
            this.btnYear.BackColor = System.Drawing.Color.Transparent;
            this.btnYear.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnYear.BtnFont = new System.Drawing.Font("微软雅黑", 12F);
            this.btnYear.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnYear.BtnText = "2019年";
            this.btnYear.ConerRadius = 5;
            this.btnYear.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnYear.Dock = System.Windows.Forms.DockStyle.Left;
            this.btnYear.FillColor = System.Drawing.Color.White;
            this.btnYear.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnYear.IsRadius = true;
            this.btnYear.IsShowRect = true;
            this.btnYear.IsShowTips = false;
            this.btnYear.Location = new System.Drawing.Point(10, 7);
            this.btnYear.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnYear.Name = "btnYear";
            this.btnYear.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnYear.RectWidth = 1;
            this.btnYear.Size = new System.Drawing.Size(80, 31);
            this.btnYear.TabIndex = 1;
            this.btnYear.TabStop = false;
            this.btnYear.TipsText = "";
            this.btnYear.BtnClick += new System.EventHandler(this.btnTime_BtnClick);
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.btnCancel);
            this.panel2.Controls.Add(this.btnOk);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel2.Location = new System.Drawing.Point(9, 300);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(497, 54);
            this.panel2.TabIndex = 2;
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.btnCancel.BackColor = System.Drawing.Color.Transparent;
            this.btnCancel.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnCancel.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
            this.btnCancel.BtnForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnCancel.BtnText = "取   消";
            this.btnCancel.ConerRadius = 5;
            this.btnCancel.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnCancel.FillColor = System.Drawing.Color.White;
            this.btnCancel.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnCancel.IsRadius = true;
            this.btnCancel.IsShowRect = true;
            this.btnCancel.IsShowTips = false;
            this.btnCancel.Location = new System.Drawing.Point(89, 9);
            this.btnCancel.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnCancel.RectWidth = 1;
            this.btnCancel.Size = new System.Drawing.Size(129, 36);
            this.btnCancel.TabIndex = 2;
            this.btnCancel.TabStop = false;
            this.btnCancel.TipsText = "";
            this.btnCancel.BtnClick += new System.EventHandler(this.btnCancel_BtnClick);
            // 
            // btnOk
            // 
            this.btnOk.Anchor = System.Windows.Forms.AnchorStyles.None;
            this.btnOk.BackColor = System.Drawing.Color.Transparent;
            this.btnOk.BtnBackColor = System.Drawing.Color.Transparent;
            this.btnOk.BtnFont = new System.Drawing.Font("微软雅黑", 13F);
            this.btnOk.BtnForeColor = System.Drawing.Color.White;
            this.btnOk.BtnText = "确  定";
            this.btnOk.ConerRadius = 5;
            this.btnOk.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnOk.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnOk.Font = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.btnOk.IsRadius = true;
            this.btnOk.IsShowRect = true;
            this.btnOk.IsShowTips = false;
            this.btnOk.Location = new System.Drawing.Point(307, 9);
            this.btnOk.Margin = new System.Windows.Forms.Padding(3, 6, 3, 6);
            this.btnOk.Name = "btnOk";
            this.btnOk.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(77)))), ((int)(((byte)(59)))));
            this.btnOk.RectWidth = 1;
            this.btnOk.Size = new System.Drawing.Size(129, 36);
            this.btnOk.TabIndex = 1;
            this.btnOk.TabStop = false;
            this.btnOk.TipsText = "";
            this.btnOk.BtnClick += new System.EventHandler(this.btnOk_BtnClick);
            // 
            // panMian
            // 
            this.panMian.Controls.Add(this.panTime);
            this.panMian.Controls.Add(this.panRight);
            this.panMian.Controls.Add(this.panLeft);
            this.panMian.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panMian.Location = new System.Drawing.Point(9, 55);
            this.panMian.Name = "panMian";
            this.panMian.Size = new System.Drawing.Size(497, 244);
            this.panMian.TabIndex = 4;
            // 
            // panTime
            // 
            this.panTime.BackColor = System.Drawing.Color.White;
            this.panTime.Column = 0;
            this.panTime.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panTime.FirstEvent = false;
            this.panTime.Location = new System.Drawing.Point(48, 0);
            this.panTime.Name = "panTime";
            this.panTime.Row = 0;
            this.panTime.SelectBtn = null;
            this.panTime.Size = new System.Drawing.Size(401, 244);
            this.panTime.Source = null;
            this.panTime.TabIndex = 0;
            // 
            // panRight
            // 
            this.panRight.BackgroundImage = global::HZH_Controls.Properties.Resources.dateRight;
            this.panRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.panRight.Dock = System.Windows.Forms.DockStyle.Right;
            this.panRight.Location = new System.Drawing.Point(449, 0);
            this.panRight.Name = "panRight";
            this.panRight.Size = new System.Drawing.Size(48, 244);
            this.panRight.TabIndex = 2;
            this.panRight.Visible = false;
            this.panRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panRight_MouseDown);
            // 
            // panLeft
            // 
            this.panLeft.BackgroundImage = global::HZH_Controls.Properties.Resources.datetLeft;
            this.panLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.panLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.panLeft.Location = new System.Drawing.Point(0, 0);
            this.panLeft.Name = "panLeft";
            this.panLeft.Size = new System.Drawing.Size(48, 244);
            this.panLeft.TabIndex = 1;
            this.panLeft.Visible = false;
            this.panLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panLeft_MouseDown);
            // 
            // ucSplitLine_H2
            // 
            this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.ucSplitLine_H2.Location = new System.Drawing.Point(9, 299);
            this.ucSplitLine_H2.Name = "ucSplitLine_H2";
            this.ucSplitLine_H2.Size = new System.Drawing.Size(497, 1);
            this.ucSplitLine_H2.TabIndex = 3;
            this.ucSplitLine_H2.TabStop = false;
            // 
            // ucSplitLine_H1
            // 
            this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
            this.ucSplitLine_H1.Location = new System.Drawing.Point(9, 54);
            this.ucSplitLine_H1.Name = "ucSplitLine_H1";
            this.ucSplitLine_H1.Size = new System.Drawing.Size(497, 1);
            this.ucSplitLine_H1.TabIndex = 1;
            this.ucSplitLine_H1.TabStop = false;
            // 
            // panel3
            // 
            this.panel3.BackColor = System.Drawing.Color.White;
            this.panel3.Controls.Add(this.panMian);
            this.panel3.Controls.Add(this.ucSplitLine_H2);
            this.panel3.Controls.Add(this.panel2);
            this.panel3.Controls.Add(this.ucSplitLine_H1);
            this.panel3.Controls.Add(this.panel1);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel3.Location = new System.Drawing.Point(1, 1);
            this.panel3.Name = "panel3";
            this.panel3.Padding = new System.Windows.Forms.Padding(9);
            this.panel3.Size = new System.Drawing.Size(515, 363);
            this.panel3.TabIndex = 5;
            // 
            // UCDateTimeSelectPan
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
            this.Controls.Add(this.panel3);
            this.Name = "UCDateTimeSelectPan";
            this.Padding = new System.Windows.Forms.Padding(1);
            this.Size = new System.Drawing.Size(517, 365);
            this.Load += new System.EventHandler(this.UCDateTimePickerExt_Load);
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panMian.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private UCSplitLine_H ucSplitLine_H1;
        private System.Windows.Forms.Panel panel2;
        private UCSplitLine_H ucSplitLine_H2;
        private System.Windows.Forms.Panel panMian;
        private UCBtnExt btnMinute;
        private UCBtnExt btnDay;
        private UCBtnExt btnHour;
        private UCBtnExt btnMonth;
        private UCBtnExt btnYear;
        private UCTimePanel panTime;
        private UCBtnExt btnCancel;
        private UCBtnExt btnOk;
        private System.Windows.Forms.Panel panRight;
        private System.Windows.Forms.Panel panLeft;
        private System.Windows.Forms.Panel sp4;
        private System.Windows.Forms.Panel sp3;
        private System.Windows.Forms.Panel sp2;
        private System.Windows.Forms.Panel sp1;
        private System.Windows.Forms.Panel panel3;

    }
}

日期输入控件

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

复制代码

完整代码

// 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCDatePickerExt.cs
// 创建日期:2019-08-15 15:59:46
// 功能描述:DateTime
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace HZH_Controls.Controls
{
    public partial class UCDatePickerExt : UCControlBase
    {
        Forms.FrmAnchor m_frmAnchor;
        UCDateTimeSelectPan m_selectPan = null;
        DateTimePickerType m_type = DateTimePickerType.DateTime;
        [Description("时间类型"), Category("自定义")]
        public DateTimePickerType TimeType
        {
            get { return m_type; }
            set
            {
                m_type = value;
                if (value == DateTimePickerType.DateTime)
                {
                    txtYear.Visible = true;
                    label1.Visible = true;
                    txtMonth.Visible = true;
                    label2.Visible = true;
                    txtDay.Visible = true;
                    label3.Visible = true;
                    txtHour.Visible = true;
                    label4.Visible = true;
                    txtMinute.Visible = true;
                    label5.Visible = true;
                }
                else if (value == DateTimePickerType.Date)
                {
                    txtYear.Visible = true;
                    label1.Visible = true;
                    txtMonth.Visible = true;
                    label2.Visible = true;
                    txtDay.Visible = true;
                    label3.Visible = true;
                    txtHour.Visible = false;
                    label4.Visible = false;
                    txtMinute.Visible = false;
                    label5.Visible = false;
                }
                else
                {
                    txtYear.Visible = false;
                    label1.Visible = false;
                    txtMonth.Visible = false;
                    label2.Visible = false;
                    txtDay.Visible = false;
                    label3.Visible = false;
                    txtHour.Visible = true;
                    label4.Visible = true;
                    txtMinute.Visible = true;
                    label5.Visible = true;
                }
            }
        }

        private DateTime currentTime = DateTime.Now;

        private int timeFontSize = 20;
        [Description("时间字体大小"), Category("自定义")]
        public int TimeFontSize
        {
            get { return timeFontSize; }
            set
            {
                if (timeFontSize != value)
                {
                    timeFontSize = value;
                    foreach (Control c in panel1.Controls)
                    {
                        c.Font = new Font(c.Font.Name, value);
                    }
                }
            }
        }

        [Description("时间"), Category("自定义")]
        public DateTime CurrentTime
        {
            get { return currentTime; }
            set
            {
                currentTime = value;
                SetTimeToControl();
            }
        }

        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');
        }
        public UCDatePickerExt()
        {
            InitializeComponent();
        }

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

        void c_MouseDown(object sender, MouseEventArgs e)
        {
            if (m_selectPan == null)
            {
                m_selectPan = new UCDateTimeSelectPan();
                m_selectPan.SelectedTimeEvent += uc_SelectedTimeEvent;
                m_selectPan.CancelTimeEvent += m_selectPan_CancelTimeEvent;
            }
            m_selectPan.CurrentTime = currentTime;
            m_selectPan.TimeType = m_type;
            m_frmAnchor = new Forms.FrmAnchor(this, m_selectPan);
            m_frmAnchor.Show(this.FindForm());
        }

        void m_selectPan_CancelTimeEvent(object sender, EventArgs e)
        {
            m_frmAnchor.Hide();
        }

        void uc_SelectedTimeEvent(object sender, EventArgs e)
        {
            CurrentTime = m_selectPan.CurrentTime;
            m_frmAnchor.Hide();
        }

        private void txtYear_TextChanged(object sender, EventArgs e)
        {
            if (txtYear.Text.Length == 4)
                this.ActiveControl = txtMonth;
        }

        private void txtMonth_TextChanged(object sender, EventArgs e)
        {
            if (txtMonth.Text.Length == 2 || txtMonth.Text.ToInt() >= 3)
            {
                this.ActiveControl = txtDay;
            }
        }

        private void txtDay_TextChanged(object sender, EventArgs e)
        {
            if (m_type == DateTimePickerType.Date)
                return;
            if (txtDay.Text.Length == 2 || txtDay.Text.ToInt() >= 4)
            {
                this.ActiveControl = txtHour;
            }
        }

        private void txtHour_TextChanged(object sender, EventArgs e)
        {
            if (txtHour.Text.Length == 2 || txtHour.Text.ToInt() >= 3)
            {
                this.ActiveControl = txtMinute;
            }
        }

        private void txtYear_Leave(object sender, EventArgs e)
        {
            if (txtYear.Text.ToInt() < 1990)
            {
                txtYear.Text = currentTime.Year.ToString();
            }
            currentTime = (txtYear.Text + currentTime.ToString("-MM-dd HH:mm:ss")).ToDate();
        }

        private void txtMonth_Leave(object sender, EventArgs e)
        {
            if (txtMonth.Text.ToInt() < 1)
            {
                txtMonth.Text = currentTime.Month.ToString().PadLeft(2, '0');
            }
            txtMonth.Text = txtMonth.Text.PadLeft(2, '0');
            currentTime = (currentTime.ToString("yyyy-" + txtMonth.Text + "-dd HH:mm:ss")).ToDate();
        }

        private void txtDay_Leave(object sender, EventArgs e)
        {
            if (txtDay.Text.ToInt() < 1 || txtDay.Text.ToInt() > DateTime.DaysInMonth(currentTime.Year, currentTime.Month))
            {
                txtDay.Text = currentTime.Day.ToString().PadLeft(2, '0');
            }
            txtDay.Text = txtDay.Text.PadLeft(2, '0');
            currentTime = (currentTime.ToString("yyyy-MM-" + txtDay.Text + " HH:mm:ss")).ToDate();
        }

        private void txtHour_Leave(object sender, EventArgs e)
        {
            if (txtHour.Text.ToInt() < 1)
            {
                txtHour.Text = currentTime.Hour.ToString().PadLeft(2, '0');
            }
            txtHour.Text = txtHour.Text.PadLeft(2, '0');
            currentTime = (currentTime.ToString("yyyy-MM-dd " + txtHour.Text + ":mm:ss")).ToDate();
        }

        private void txtMinute_Leave(object sender, EventArgs e)
        {
            if (txtMinute.Text.ToInt() < 1)
            {
                txtMinute.Text = currentTime.Minute.ToString().PadLeft(2, '0');
            }
            txtMinute.Text = txtMinute.Text.PadLeft(2, '0');
            currentTime = (currentTime.ToString("yyyy-MM-dd HH:" + txtMinute.Text + ":ss")).ToDate();
        }

        private void txt_SizeChanged(object sender, EventArgs e)
        {
            panel1.Height = (sender as TextBoxEx).Height;
        }
    }
}
namespace HZH_Controls.Controls
{
    partial class UCDatePickerExt
    {
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.txtMinute = new HZH_Controls.Controls.TextBoxEx();
            this.label4 = new System.Windows.Forms.Label();
            this.txtHour = new HZH_Controls.Controls.TextBoxEx();
            this.label3 = new System.Windows.Forms.Label();
            this.txtDay = new HZH_Controls.Controls.TextBoxEx();
            this.label2 = new System.Windows.Forms.Label();
            this.txtMonth = new HZH_Controls.Controls.TextBoxEx();
            this.label1 = new System.Windows.Forms.Label();
            this.txtYear = new HZH_Controls.Controls.TextBoxEx();
            this.label5 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.Controls.Add(this.label5);
            this.panel1.Controls.Add(this.txtMinute);
            this.panel1.Controls.Add(this.label4);
            this.panel1.Controls.Add(this.txtHour);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.txtDay);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.txtMonth);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.txtYear);
            this.panel1.Location = new System.Drawing.Point(3, 6);
            this.panel1.MaximumSize = new System.Drawing.Size(0, 27);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(339, 27);
            this.panel1.TabIndex = 9;
            // 
            // txtMinute
            // 
            this.txtMinute.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtMinute.DecLength = 2;
            this.txtMinute.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtMinute.Font = new System.Drawing.Font("Arial Unicode MS", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtMinute.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.txtMinute.InputType = TextInputType.Integer;
            this.txtMinute.Location = new System.Drawing.Point(272, 0);
            this.txtMinute.MaxValue = new decimal(new int[] {
            59,
            0,
            0,
            0});
            this.txtMinute.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtMinute.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.txtMinute.Name = "txtMinute";
            this.txtMinute.OldText = null;
            this.txtMinute.PromptColor = System.Drawing.Color.Gray;
            this.txtMinute.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtMinute.PromptText = "";
            this.txtMinute.RegexPattern = "";
            this.txtMinute.Size = new System.Drawing.Size(29, 27);
            this.txtMinute.TabIndex = 5;
            this.txtMinute.Text = "59";
            this.txtMinute.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtMinute.SizeChanged += new System.EventHandler(this.txt_SizeChanged);
            this.txtMinute.Leave += new System.EventHandler(this.txtMinute_Leave);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Dock = System.Windows.Forms.DockStyle.Left;
            this.label4.Font = new System.Drawing.Font("微软雅黑", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.label4.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.label4.Location = new System.Drawing.Point(240, 0);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(32, 27);
            this.label4.TabIndex = 16;
            this.label4.Text = "时";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtHour
            // 
            this.txtHour.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtHour.DecLength = 2;
            this.txtHour.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtHour.Font = new System.Drawing.Font("Arial Unicode MS", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtHour.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.txtHour.InputType = TextInputType.Integer;
            this.txtHour.Location = new System.Drawing.Point(211, 0);
            this.txtHour.MaxValue = new decimal(new int[] {
            23,
            0,
            0,
            0});
            this.txtHour.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtHour.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.txtHour.Name = "txtHour";
            this.txtHour.OldText = null;
            this.txtHour.PromptColor = System.Drawing.Color.Gray;
            this.txtHour.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtHour.PromptText = "";
            this.txtHour.RegexPattern = "";
            this.txtHour.Size = new System.Drawing.Size(29, 27);
            this.txtHour.TabIndex = 4;
            this.txtHour.Text = "23";
            this.txtHour.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtHour.SizeChanged += new System.EventHandler(this.txt_SizeChanged);
            this.txtHour.TextChanged += new System.EventHandler(this.txtHour_TextChanged);
            this.txtHour.Leave += new System.EventHandler(this.txtHour_Leave);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Dock = System.Windows.Forms.DockStyle.Left;
            this.label3.Font = new System.Drawing.Font("微软雅黑", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.label3.Location = new System.Drawing.Point(173, 0);
            this.label3.Margin = new System.Windows.Forms.Padding(0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 27);
            this.label3.TabIndex = 14;
            this.label3.Text = " 日";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtDay
            // 
            this.txtDay.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtDay.DecLength = 2;
            this.txtDay.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtDay.Font = new System.Drawing.Font("Arial Unicode MS", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtDay.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.txtDay.InputType = TextInputType.Integer;
            this.txtDay.Location = new System.Drawing.Point(144, 0);
            this.txtDay.MaxValue = new decimal(new int[] {
            31,
            0,
            0,
            0});
            this.txtDay.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtDay.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.txtDay.Name = "txtDay";
            this.txtDay.OldText = null;
            this.txtDay.PromptColor = System.Drawing.Color.Gray;
            this.txtDay.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtDay.PromptText = "";
            this.txtDay.RegexPattern = "";
            this.txtDay.Size = new System.Drawing.Size(29, 27);
            this.txtDay.TabIndex = 3;
            this.txtDay.Text = "12";
            this.txtDay.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtDay.SizeChanged += new System.EventHandler(this.txt_SizeChanged);
            this.txtDay.TextChanged += new System.EventHandler(this.txtDay_TextChanged);
            this.txtDay.Leave += new System.EventHandler(this.txtDay_Leave);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Dock = System.Windows.Forms.DockStyle.Left;
            this.label2.Font = new System.Drawing.Font("微软雅黑", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.label2.Location = new System.Drawing.Point(112, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(32, 27);
            this.label2.TabIndex = 12;
            this.label2.Text = "月";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtMonth
            // 
            this.txtMonth.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtMonth.DecLength = 2;
            this.txtMonth.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtMonth.Font = new System.Drawing.Font("Arial Unicode MS", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtMonth.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.txtMonth.InputType = TextInputType.Integer;
            this.txtMonth.Location = new System.Drawing.Point(83, 0);
            this.txtMonth.MaxValue = new decimal(new int[] {
            12,
            0,
            0,
            0});
            this.txtMonth.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtMonth.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.txtMonth.Name = "txtMonth";
            this.txtMonth.OldText = null;
            this.txtMonth.PromptColor = System.Drawing.Color.Gray;
            this.txtMonth.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtMonth.PromptText = "";
            this.txtMonth.RegexPattern = "";
            this.txtMonth.Size = new System.Drawing.Size(29, 27);
            this.txtMonth.TabIndex = 2;
            this.txtMonth.Text = "12";
            this.txtMonth.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtMonth.SizeChanged += new System.EventHandler(this.txt_SizeChanged);
            this.txtMonth.TextChanged += new System.EventHandler(this.txtMonth_TextChanged);
            this.txtMonth.Leave += new System.EventHandler(this.txtMonth_Leave);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Dock = System.Windows.Forms.DockStyle.Left;
            this.label1.Font = new System.Drawing.Font("微软雅黑", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.label1.Location = new System.Drawing.Point(51, 0);
            this.label1.Margin = new System.Windows.Forms.Padding(0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 27);
            this.label1.TabIndex = 10;
            this.label1.Text = "年";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtYear
            // 
            this.txtYear.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtYear.DecLength = 2;
            this.txtYear.Dock = System.Windows.Forms.DockStyle.Left;
            this.txtYear.Font = new System.Drawing.Font("Arial Unicode MS", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtYear.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.txtYear.InputType = TextInputType.Integer;
            this.txtYear.Location = new System.Drawing.Point(0, 0);
            this.txtYear.MaxValue = new decimal(new int[] {
            2099,
            0,
            0,
            0});
            this.txtYear.MinValue = new decimal(new int[] {
            0,
            0,
            0,
            0});
            this.txtYear.MyRectangle = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.txtYear.Name = "txtYear";
            this.txtYear.OldText = null;
            this.txtYear.PromptColor = System.Drawing.Color.Gray;
            this.txtYear.PromptFont = new System.Drawing.Font("微软雅黑", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.txtYear.PromptText = "";
            this.txtYear.RegexPattern = "";
            this.txtYear.Size = new System.Drawing.Size(51, 27);
            this.txtYear.TabIndex = 1;
            this.txtYear.Text = "2019";
            this.txtYear.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtYear.SizeChanged += new System.EventHandler(this.txt_SizeChanged);
            this.txtYear.TextChanged += new System.EventHandler(this.txtYear_TextChanged);
            this.txtYear.Leave += new System.EventHandler(this.txtYear_Leave);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Dock = System.Windows.Forms.DockStyle.Left;
            this.label5.Font = new System.Drawing.Font("微软雅黑", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel);
            this.label5.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(60)))), ((int)(((byte)(60)))), ((int)(((byte)(60)))));
            this.label5.Location = new System.Drawing.Point(301, 0);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(32, 27);
            this.label5.TabIndex = 17;
            this.label5.Text = "分";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // UCDatePickerExt
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.BackColor = System.Drawing.Color.Transparent;
            this.ConerRadius = 5;
            this.Controls.Add(this.panel1);
            this.IsShowRect = true;
            this.IsRadius = true;
            this.Name = "UCDatePickerExt";
            this.Padding = new System.Windows.Forms.Padding(0, 10, 0, 0);
            this.Size = new System.Drawing.Size(345, 39);
            this.Load += new System.EventHandler(this.UCDatePickerExt_Load);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label1;
        private TextBoxEx txtMinute;
        private TextBoxEx txtHour;
        private TextBoxEx txtDay;
        private TextBoxEx txtMonth;
        private TextBoxEx txtYear;
        private System.Windows.Forms.Label label5;
    }
}

用处及效果

 

 

最后的话

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

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐