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

时间:2023-03-09 07:20:53
(三十三)c#Winform自定义控件-日期控件

官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">(三十三)c#Winform自定义控件-日期控件

目录

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

准备工作

日期控件将分为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(, , );
}
bool blnEvent = FirstEvent ? true : (selectBtn != null);
selectBtn = value;
if (value != null)
{
selectBtn.FillColor = System.Drawing.Color.FromArgb(, , );
selectBtn.RectColor = System.Drawing.Color.FromArgb(, , );
selectBtn.BtnForeColor = System.Drawing.Color.White;
if (blnEvent && SelectSourceEvent != null)
SelectSourceEvent(selectBtn.Tag.ToStringExt(), null);
}
}
}
private int row = ; public int Row
{
get { return row; }
set
{
row = value;
ReloadPanel();
}
} private int column = ; 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 <= || column <= )
return;
if (Source != lstSource)
Source = lstSource;
int index = ;
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 <= || column <= )
return;
SelectBtn = null;
this.panMain.Controls.Clear();
this.panMain.ColumnCount = column;
this.panMain.ColumnStyles.Clear();
for (int i = ; 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 = ; i < row; i++)
{
this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
} for (int i = ; i < row; i++)
{
for (int j = ; 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(, , );
btn.ConerRadius = ;
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 = ;
btn.Width = this.Width;
btn.TabIndex = ;
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(, , );
}
bool blnEvent = FirstEvent ? true : (selectBtn != null);
selectBtn = value;
if (value != null)
{
selectBtn.FillColor = System.Drawing.Color.FromArgb(, , );
selectBtn.RectColor = System.Drawing.Color.FromArgb(, , );
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 = ; public int Row
{
get { return row; }
set
{
row = value;
ReloadPanel();
}
} private int column = ; 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 <= || column <= )
return;
if (Source != lstSource)
Source = lstSource;
int index = ;
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 <= || column <= )
return;
SelectBtn = null;
this.panMain.Controls.Clear();
this.panMain.ColumnCount = column;
this.panMain.ColumnStyles.Clear();
for (int i = ; 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 = ; i < row; i++)
{
this.panMain.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
} for (int i = ; i < row; i++)
{
for (int j = ; 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(, , );
btn.ConerRadius = ;
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 = ;
btn.Width = this.Width;
btn.TabIndex = ;
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 = ;
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(, );
this.panMain.Name = "panMain";
this.panMain.RowCount = ;
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(, );
this.panMain.TabIndex = ;
//
// ucSplitLine_V1
//
this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
this.ucSplitLine_V1.Location = new System.Drawing.Point(, );
this.ucSplitLine_V1.Name = "ucSplitLine_V1";
this.ucSplitLine_V1.Size = new System.Drawing.Size(, );
this.ucSplitLine_V1.TabIndex = ;
this.ucSplitLine_V1.TabStop = false;
this.ucSplitLine_V1.Visible = false;
//
// ucSplitLine_V2
//
this.ucSplitLine_V2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_V2.Dock = System.Windows.Forms.DockStyle.Right;
this.ucSplitLine_V2.Location = new System.Drawing.Point(, );
this.ucSplitLine_V2.Name = "ucSplitLine_V2";
this.ucSplitLine_V2.Size = new System.Drawing.Size(, );
this.ucSplitLine_V2.TabIndex = ;
this.ucSplitLine_V2.TabStop = false;
this.ucSplitLine_V2.Visible = false;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
this.ucSplitLine_H1.Location = new System.Drawing.Point(, );
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(, );
this.ucSplitLine_H1.TabIndex = ;
this.ucSplitLine_H1.TabStop = false;
this.ucSplitLine_H1.Visible = false;
//
// ucSplitLine_H2
//
this.ucSplitLine_H2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H2.Location = new System.Drawing.Point(, );
this.ucSplitLine_H2.Name = "ucSplitLine_H2";
this.ucSplitLine_H2.Size = new System.Drawing.Size(, );
this.ucSplitLine_H2.TabIndex = ;
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(, );
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 <= )
{
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(, '') + "月";
btnDay.Tag = m_dt.Day;
btnDay.BtnText = m_dt.Day.ToString().PadLeft(, '') + "日";
btnHour.Tag = m_dt.Hour;
btnHour.BtnText = m_dt.Hour.ToString().PadLeft(, '') + "时";
btnMinute.Tag = m_dt.Minute;
btnMinute.BtnText = m_dt.Minute.ToString().PadLeft(, '') + "分";
} private void SetSelectType(UCBtnExt btn)
{
try
{
ControlHelper.FreezeControl(this, true);
if (m_thisBtn != null)
{
m_thisBtn.FillColor = Color.White;
m_thisBtn.BtnForeColor = Color.FromArgb(, , );
}
m_thisBtn = btn;
if (m_thisBtn != null)
{
m_thisBtn.FillColor = Color.FromArgb(, , );
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 = ;
panTime.Column = ;
int intYear = m_dt.Year - m_dt.Year % ;
for (int i = ; i < ; i++)
{
lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
}
}
else if (btn == btnMonth)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '') + "月\r\n" + (("2019-" + i + "-01").ToDate().ToString("MMM", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")))));
}
}
else if (btn == btnDay)
{
panTime.Column = ;
int intDayCount = DateTime.DaysInMonth(m_dt.Year, m_dt.Month);
int intIndex = (int)(m_dt.DayOfWeek);
panTime.Row = (intDayCount + intIndex) / + ((intDayCount + intIndex) % != ? : );
for (int i = ; i < intIndex; i++)
{
lstSource.Add(new KeyValuePair<string, string>("", ""));
}
for (int i = ; i <= intDayCount; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '')));
}
}
else if (btn == btnHour)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString() + "时"));
}
}
else if (btn == btnMinute)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '')));
}
}
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[].Key.ToInt() - this.panTime.Source[].Key.ToInt() % - ;
panTime.SuspendLayout();
panTime.Row = ;
panTime.Column = ;
for (int i = ; i < ; 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[].Key.ToInt() - this.panTime.Source[].Key.ToInt() % + ;
panTime.SuspendLayout();
panTime.Row = ;
panTime.Column = ;
for (int i = ; i < ; 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 <= )
{
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(, '') + "月";
btnDay.Tag = m_dt.Day;
btnDay.BtnText = m_dt.Day.ToString().PadLeft(, '') + "日";
btnHour.Tag = m_dt.Hour;
btnHour.BtnText = m_dt.Hour.ToString().PadLeft(, '') + "时";
btnMinute.Tag = m_dt.Minute;
btnMinute.BtnText = m_dt.Minute.ToString().PadLeft(, '') + "分";
} private void SetSelectType(UCBtnExt btn)
{
try
{
ControlHelper.FreezeControl(this, true);
if (m_thisBtn != null)
{
m_thisBtn.FillColor = Color.White;
m_thisBtn.BtnForeColor = Color.FromArgb(, , );
}
m_thisBtn = btn;
if (m_thisBtn != null)
{
m_thisBtn.FillColor = Color.FromArgb(, , );
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 = ;
panTime.Column = ;
int intYear = m_dt.Year - m_dt.Year % ;
for (int i = ; i < ; i++)
{
lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
}
}
else if (btn == btnMonth)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '') + "月\r\n" + (("2019-" + i + "-01").ToDate().ToString("MMM", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")))));
}
}
else if (btn == btnDay)
{
panTime.Column = ;
int intDayCount = DateTime.DaysInMonth(m_dt.Year, m_dt.Month);
int intIndex = (int)(m_dt.DayOfWeek);
panTime.Row = (intDayCount + intIndex) / + ((intDayCount + intIndex) % != ? : );
for (int i = ; i < intIndex; i++)
{
lstSource.Add(new KeyValuePair<string, string>("", ""));
}
for (int i = ; i <= intDayCount; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '')));
}
}
else if (btn == btnHour)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString() + "时"));
}
}
else if (btn == btnMinute)
{
panTime.Row = ;
panTime.Column = ;
for (int i = ; i <= ; i++)
{
lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(, '')));
}
}
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[].Key.ToInt() - this.panTime.Source[].Key.ToInt() % - ;
panTime.SuspendLayout();
panTime.Row = ;
panTime.Column = ;
for (int i = ; i < ; 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[].Key.ToInt() - this.panTime.Source[].Key.ToInt() % + ;
panTime.SuspendLayout();
panTime.Row = ;
panTime.Column = ;
for (int i = ; i < ; 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(, );
this.panel1.Name = "panel1";
this.panel1.Padding = new System.Windows.Forms.Padding(, , , );
this.panel1.Size = new System.Drawing.Size(, );
this.panel1.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnMinute.BtnText = "30分";
this.btnMinute.ConerRadius = ;
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(, );
this.btnMinute.Margin = new System.Windows.Forms.Padding(, , , );
this.btnMinute.Name = "btnMinute";
this.btnMinute.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnMinute.RectWidth = ;
this.btnMinute.Size = new System.Drawing.Size(, );
this.btnMinute.TabIndex = ;
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(, );
this.sp4.Name = "sp4";
this.sp4.Size = new System.Drawing.Size(, );
this.sp4.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnHour.BtnText = "12时";
this.btnHour.ConerRadius = ;
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(, );
this.btnHour.Margin = new System.Windows.Forms.Padding(, , , );
this.btnHour.Name = "btnHour";
this.btnHour.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnHour.RectWidth = ;
this.btnHour.Size = new System.Drawing.Size(, );
this.btnHour.TabIndex = ;
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(, );
this.sp3.Name = "sp3";
this.sp3.Size = new System.Drawing.Size(, );
this.sp3.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnDay.BtnText = "30日";
this.btnDay.ConerRadius = ;
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(, );
this.btnDay.Margin = new System.Windows.Forms.Padding(, , , );
this.btnDay.Name = "btnDay";
this.btnDay.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnDay.RectWidth = ;
this.btnDay.Size = new System.Drawing.Size(, );
this.btnDay.TabIndex = ;
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(, );
this.sp2.Name = "sp2";
this.sp2.Size = new System.Drawing.Size(, );
this.sp2.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnMonth.BtnText = "12月";
this.btnMonth.ConerRadius = ;
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(, );
this.btnMonth.Margin = new System.Windows.Forms.Padding(, , , );
this.btnMonth.Name = "btnMonth";
this.btnMonth.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnMonth.RectWidth = ;
this.btnMonth.Size = new System.Drawing.Size(, );
this.btnMonth.TabIndex = ;
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(, );
this.sp1.Name = "sp1";
this.sp1.Size = new System.Drawing.Size(, );
this.sp1.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnYear.BtnText = "2019年";
this.btnYear.ConerRadius = ;
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(, );
this.btnYear.Margin = new System.Windows.Forms.Padding(, , , );
this.btnYear.Name = "btnYear";
this.btnYear.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnYear.RectWidth = ;
this.btnYear.Size = new System.Drawing.Size(, );
this.btnYear.TabIndex = ;
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(, );
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(, );
this.panel2.TabIndex = ;
//
// 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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnCancel.BtnText = "取 消";
this.btnCancel.ConerRadius = ;
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(, );
this.btnCancel.Margin = new System.Windows.Forms.Padding(, , , );
this.btnCancel.Name = "btnCancel";
this.btnCancel.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnCancel.RectWidth = ;
this.btnCancel.Size = new System.Drawing.Size(, );
this.btnCancel.TabIndex = ;
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 = ;
this.btnOk.Cursor = System.Windows.Forms.Cursors.Hand;
this.btnOk.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
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(, );
this.btnOk.Margin = new System.Windows.Forms.Padding(, , , );
this.btnOk.Name = "btnOk";
this.btnOk.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.btnOk.RectWidth = ;
this.btnOk.Size = new System.Drawing.Size(, );
this.btnOk.TabIndex = ;
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(, );
this.panMian.Name = "panMian";
this.panMian.Size = new System.Drawing.Size(, );
this.panMian.TabIndex = ;
//
// panTime
//
this.panTime.BackColor = System.Drawing.Color.White;
this.panTime.Column = ;
this.panTime.Dock = System.Windows.Forms.DockStyle.Fill;
this.panTime.FirstEvent = false;
this.panTime.Location = new System.Drawing.Point(, );
this.panTime.Name = "panTime";
this.panTime.Row = ;
this.panTime.SelectBtn = null;
this.panTime.Size = new System.Drawing.Size(, );
this.panTime.Source = null;
this.panTime.TabIndex = ;
//
// 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(, );
this.panRight.Name = "panRight";
this.panRight.Size = new System.Drawing.Size(, );
this.panRight.TabIndex = ;
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(, );
this.panLeft.Name = "panLeft";
this.panLeft.Size = new System.Drawing.Size(, );
this.panLeft.TabIndex = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H2.Location = new System.Drawing.Point(, );
this.ucSplitLine_H2.Name = "ucSplitLine_H2";
this.ucSplitLine_H2.Size = new System.Drawing.Size(, );
this.ucSplitLine_H2.TabIndex = ;
this.ucSplitLine_H2.TabStop = false;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Top;
this.ucSplitLine_H1.Location = new System.Drawing.Point(, );
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(, );
this.ucSplitLine_H1.TabIndex = ;
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(, );
this.panel3.Name = "panel3";
this.panel3.Padding = new System.Windows.Forms.Padding();
this.panel3.Size = new System.Drawing.Size(, );
this.panel3.TabIndex = ;
//
// UCDateTimeSelectPan
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.Controls.Add(this.panel3);
this.Name = "UCDateTimeSelectPan";
this.Padding = new System.Windows.Forms.Padding();
this.Size = new System.Drawing.Size(, );
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 = ;
[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(, '');
this.txtDay.Text = currentTime.Day.ToString().PadLeft(, '');
this.txtHour.Text = currentTime.Hour.ToString().PadLeft(, '');
this.txtMinute.Text = currentTime.Minute.ToString().PadLeft(, '');
} 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 == )
this.ActiveControl = txtMonth;
} private void txtMonth_TextChanged(object sender, EventArgs e)
{
if (txtMonth.Text.Length == || txtMonth.Text.ToInt() >= )
{
this.ActiveControl = txtDay;
}
} private void txtDay_TextChanged(object sender, EventArgs e)
{
if (m_type == DateTimePickerType.Date)
return;
if (txtDay.Text.Length == || txtDay.Text.ToInt() >= )
{
this.ActiveControl = txtHour;
}
} private void txtHour_TextChanged(object sender, EventArgs e)
{
if (txtHour.Text.Length == || txtHour.Text.ToInt() >= )
{
this.ActiveControl = txtMinute;
}
} private void txtYear_Leave(object sender, EventArgs e)
{
if (txtYear.Text.ToInt() < )
{
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() < )
{
txtMonth.Text = currentTime.Month.ToString().PadLeft(, '');
}
txtMonth.Text = txtMonth.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-" + txtMonth.Text + "-dd HH:mm:ss")).ToDate();
} private void txtDay_Leave(object sender, EventArgs e)
{
if (txtDay.Text.ToInt() < || txtDay.Text.ToInt() > DateTime.DaysInMonth(currentTime.Year, currentTime.Month))
{
txtDay.Text = currentTime.Day.ToString().PadLeft(, '');
}
txtDay.Text = txtDay.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-MM-" + txtDay.Text + " HH:mm:ss")).ToDate();
} private void txtHour_Leave(object sender, EventArgs e)
{
if (txtHour.Text.ToInt() < )
{
txtHour.Text = currentTime.Hour.ToString().PadLeft(, '');
}
txtHour.Text = txtHour.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-MM-dd " + txtHour.Text + ":mm:ss")).ToDate();
} private void txtMinute_Leave(object sender, EventArgs e)
{
if (txtMinute.Text.ToInt() < )
{
txtMinute.Text = currentTime.Minute.ToString().PadLeft(, '');
}
txtMinute.Text = txtMinute.Text.PadLeft(, '');
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 = ;
[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(, '');
this.txtDay.Text = currentTime.Day.ToString().PadLeft(, '');
this.txtHour.Text = currentTime.Hour.ToString().PadLeft(, '');
this.txtMinute.Text = currentTime.Minute.ToString().PadLeft(, '');
}
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 == )
this.ActiveControl = txtMonth;
} private void txtMonth_TextChanged(object sender, EventArgs e)
{
if (txtMonth.Text.Length == || txtMonth.Text.ToInt() >= )
{
this.ActiveControl = txtDay;
}
} private void txtDay_TextChanged(object sender, EventArgs e)
{
if (m_type == DateTimePickerType.Date)
return;
if (txtDay.Text.Length == || txtDay.Text.ToInt() >= )
{
this.ActiveControl = txtHour;
}
} private void txtHour_TextChanged(object sender, EventArgs e)
{
if (txtHour.Text.Length == || txtHour.Text.ToInt() >= )
{
this.ActiveControl = txtMinute;
}
} private void txtYear_Leave(object sender, EventArgs e)
{
if (txtYear.Text.ToInt() < )
{
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() < )
{
txtMonth.Text = currentTime.Month.ToString().PadLeft(, '');
}
txtMonth.Text = txtMonth.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-" + txtMonth.Text + "-dd HH:mm:ss")).ToDate();
} private void txtDay_Leave(object sender, EventArgs e)
{
if (txtDay.Text.ToInt() < || txtDay.Text.ToInt() > DateTime.DaysInMonth(currentTime.Year, currentTime.Month))
{
txtDay.Text = currentTime.Day.ToString().PadLeft(, '');
}
txtDay.Text = txtDay.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-MM-" + txtDay.Text + " HH:mm:ss")).ToDate();
} private void txtHour_Leave(object sender, EventArgs e)
{
if (txtHour.Text.ToInt() < )
{
txtHour.Text = currentTime.Hour.ToString().PadLeft(, '');
}
txtHour.Text = txtHour.Text.PadLeft(, '');
currentTime = (currentTime.ToString("yyyy-MM-dd " + txtHour.Text + ":mm:ss")).ToDate();
} private void txtMinute_Leave(object sender, EventArgs e)
{
if (txtMinute.Text.ToInt() < )
{
txtMinute.Text = currentTime.Minute.ToString().PadLeft(, '');
}
txtMinute.Text = txtMinute.Text.PadLeft(, '');
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(, );
this.panel1.MaximumSize = new System.Drawing.Size(, );
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(, );
this.panel1.TabIndex = ;
//
// txtMinute
//
this.txtMinute.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtMinute.DecLength = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtMinute.InputType = TextInputType.Integer;
this.txtMinute.Location = new System.Drawing.Point(, );
this.txtMinute.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtMinute.MinValue = new decimal(new int[] {
,
,
,
});
this.txtMinute.MyRectangle = new System.Drawing.Rectangle(, , , );
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(, );
this.txtMinute.TabIndex = ;
this.txtMinute.Text = "";
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.label4.Location = new System.Drawing.Point(, );
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(, );
this.label4.TabIndex = ;
this.label4.Text = "时";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtHour
//
this.txtHour.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtHour.DecLength = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtHour.InputType = TextInputType.Integer;
this.txtHour.Location = new System.Drawing.Point(, );
this.txtHour.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtHour.MinValue = new decimal(new int[] {
,
,
,
});
this.txtHour.MyRectangle = new System.Drawing.Rectangle(, , , );
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(, );
this.txtHour.TabIndex = ;
this.txtHour.Text = "";
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(, );
this.label3.Margin = new System.Windows.Forms.Padding();
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(, );
this.label3.TabIndex = ;
this.label3.Text = " 日";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtDay
//
this.txtDay.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtDay.DecLength = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtDay.InputType = TextInputType.Integer;
this.txtDay.Location = new System.Drawing.Point(, );
this.txtDay.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtDay.MinValue = new decimal(new int[] {
,
,
,
});
this.txtDay.MyRectangle = new System.Drawing.Rectangle(, , , );
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(, );
this.txtDay.TabIndex = ;
this.txtDay.Text = "";
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.label2.Location = new System.Drawing.Point(, );
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(, );
this.label2.TabIndex = ;
this.label2.Text = "月";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtMonth
//
this.txtMonth.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtMonth.DecLength = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtMonth.InputType = TextInputType.Integer;
this.txtMonth.Location = new System.Drawing.Point(, );
this.txtMonth.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtMonth.MinValue = new decimal(new int[] {
,
,
,
});
this.txtMonth.MyRectangle = new System.Drawing.Rectangle(, , , );
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(, );
this.txtMonth.TabIndex = ;
this.txtMonth.Text = "";
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.label1.Location = new System.Drawing.Point(, );
this.label1.Margin = new System.Windows.Forms.Padding();
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(, );
this.label1.TabIndex = ;
this.label1.Text = "年";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtYear
//
this.txtYear.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txtYear.DecLength = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.txtYear.InputType = TextInputType.Integer;
this.txtYear.Location = new System.Drawing.Point(, );
this.txtYear.MaxValue = new decimal(new int[] {
,
,
,
});
this.txtYear.MinValue = new decimal(new int[] {
,
,
,
});
this.txtYear.MyRectangle = new System.Drawing.Rectangle(, , , );
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(, );
this.txtYear.TabIndex = ;
this.txtYear.Text = "";
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.label5.Location = new System.Drawing.Point(, );
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(, );
this.label5.TabIndex = ;
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 = ;
this.Controls.Add(this.panel1);
this.IsShowRect = true;
this.IsRadius = true;
this.Name = "UCDatePickerExt";
this.Padding = new System.Windows.Forms.Padding(, , , );
this.Size = new System.Drawing.Size(, );
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;
}
}

用处及效果

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

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

最后的话

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