官网

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">

目录

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

用处及效果

最后的话

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

(三十三)c#Winform自定义控件-日期控件的更多相关文章

  1. (三十六)c#Winform自定义控件-步骤控件-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  2. (十二)c#Winform自定义控件-分页控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (七十七)c#Winform自定义控件-采样控件-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. winform自定义日期控件,要求可以手动输入日期DatePicker

    要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...

  5. WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 日历控 ...

  6. 【转】WPF自定义控件与样式(5)-Calendar/DatePicker日期控件自定义样式及扩展

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 日历控件Calendar自定义样式: 日期控件DatePicker自定 ...

  7. UI自动化之特殊处理三(日期控件\表格\富文本)

    日期控件\表格\富文本也是一些常遇到的需要特殊处理的定位 目录 1.日期控件 2.表格 3.富文本 1.日期控件 第一种:输入框属性为:readonly="readonly" # ...

  8. WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一)

    WinForm界面布局控件WeifenLuo.WinFormsUI.Docking"的使用 (一) 编写人:CC阿爸 2015-1-28 在伍华聪的博客中,看到布局控件"Weife ...

  9. jQuery Datepicker日期控件

    datepicker可以为bootstrap添加一个事件选择控件,适用于任何需要调用的场合,支持多种事件格式输出(比如:dd, d, mm, m, yyyy, yy等),是制作网页不可缺失的插件. R ...

随机推荐

  1. json字符串转换成java对象

  2. Bzoj1972: [Sdoi2010]猪国杀 题解(大模拟+耐心+细心)

    猪国杀 - 可读版本 https://mubu.com/doc/2707815814591da4 题目可真长,读题都要一个小时. 这道题很多人都说不可做,耗时间,代码量大,于是,本着不做死就不会死的精 ...

  3. HTML&CSS构建和设计网站

    1.a链接:两个特性,href.target,href里添加mailto:邮箱可以直接链接到其它人的邮箱,添加#id,可以把窗口位置直接定位到该id的元素位置, target:_black可以在新的窗 ...

  4. tomcat不需要重启热部署xml文件

    项目中,遇到情况,有时候增加struts的配置了,有时候粗心改错了,然后急需要发布线上吧,又不能重启影响其他的,最后发现struts有这个功能呢! 在struts.xml的配置文件中加上一句话就行 & ...

  5. STM32-I2C_CheckEvent-标志位自动清除理解

    STM32里I2C_CheckEvent函数我们应该是相当熟悉了,在每次发送数据后我们都需要检验相应的EVx(x = 0,1,2,,,)事件是否有发送. 函数定义如下: ErrorStatus I2C ...

  6. 利用百度AI OCR图片识别,Java实现PDF中的图片转换成文字

    序言:我们在读一些PDF版书籍的时候,如果PDF中不是图片,做起读书笔记的还好:如果PDF中的是图片的话,根本无法编辑,做起笔记来,还是很痛苦的.我是遇到过了.我们搞技术的,当然得自己学着解决现在的痛 ...

  7. MYSQL数据库的安装,配置文件,登入

    07.13自我总结 MYSQL数据库 一.MYQL数据库的安装 可以去mysql官网下载mysql压缩包 运行程序:在bin文件夹中,其中客户端运行文件是mysql.exe,服务端运行文件为mysql ...

  8. php curl问题汇总

    0. curl是个什么东西 复制代码代码如下: PHP supports libcurl, a library created by Daniel Stenberg, that allows you ...

  9. PHP正则匹配到2个字符串之间的内容,匹配HTML便签内容

    PHP正则匹配到2个字符串之间的内容 $preg= '/xue[\s\S]*?om/i'; preg_match_all($preg,"学并思网址xuebingsi.com",$r ...

  10. python基础——列表(list)

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...