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

准备工作

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

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

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

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

开始

添加用户控件,命名UCTimePanel

属性

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

一些公共函数

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

全部代码

// 版权所有  黄正辉  交流群: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

属性

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

2个构造函数

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') + "月rn" + (("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);
            }
        }

完整代码

// 版权所有  黄正辉  交流群: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') + "月rn" + (("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

属性

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

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

完整代码

// 版权所有  黄正辉  交流群: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 点个星 星吧

作者:冰封一夏
出处: http://www.hzhcontrols.com/doc.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,
且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

原文出处:冰封一夏

原文链接:https://www.cnblogs.com/bfyx/p/11364838.html

本文观点不代表Dotnet9立场,转载请联系原作者。

发表评论

登录后才能评论