using System; using System.ComponentModel; using System.Windows.Forms; namespace HL_FristAidPlatform_Public { public partial class TimeControl : UserControl { #region 用户自定义属性 #region 私有属性 /// /// 日期格式 /// 私有属性,根据FormatString_Date产生值 /// private string FormatString = "yyyy-MM-dd HH:mm"; /// /// 时间 /// 私有属性,根据FormatString_Time产生值 /// private string EditMask = "HH:mm"; #endregion 私有属性 #region 控件公有属性 调用端使用 /// /// 时间文字值 /// public string TimeValue { get { if (string.IsNullOrEmpty(dateEdit.EditValue + "")) { return PublicClass.ToString(dateEdit.EditValue, ""); } else { return Convert.ToDateTime(PublicClass.ToString(dateEdit.EditValue, "")).ToString(FormatString); } } set { if (string.IsNullOrWhiteSpace(value)) { dateEdit.EditValue = ""; } else { string time = Convert.ToDateTime(value).ToString(FormatString); dateEdit.EditValue = time; } } } /// /// 控件输入框中日期+时间 格式 /// [DefaultValue(typeof(DateFormat), "yyyyMMddHHmm")] private DateFormat formatString_Date; public DateFormat FormatString_Date { get { return formatString_Date; } set { switch (value) { case DateFormat.yyyyMM: FormatString = "yyyy年MM月"; break; case DateFormat.yyyyMMdd: FormatString = "yyyy-MM-dd"; break; case DateFormat.yyyyMMddHHmm: FormatString = "yyyy-MM-dd HH:mm"; break; case DateFormat.yyyyMMddHHmmss: FormatString = "yyyy-MM-dd HH:mm:ss"; break; default: FormatString = "yyyy-MM-dd HH:mm"; break; } formatString_Date = value; ChangeFormatString(); } } /// /// 控件点开后时钟下输入框中的 格式 /// [DefaultValue(typeof(TimeFormat), "HHmm")] private TimeFormat formatString_Time; public TimeFormat FormatString_Time { get { return formatString_Time; } set { switch (value) { case TimeFormat.HHmm: EditMask = "HH:mm"; break; case TimeFormat.HHmmss: EditMask = "HH:mm:ss"; break; default: EditMask = "HH:mm"; break; } formatString_Time = value; } } /// /// 是否显示时钟 /// public DevExpress.Utils.DefaultBoolean defaultBoolean { get; set; } /// /// 日期格式化枚举 /// public enum DateFormat { yyyyMM = 1, yyyyMMdd = 2, yyyyMMddHHmm = 3, yyyyMMddHHmmss = 4, } /// /// 时间格式化枚举 /// public enum TimeFormat { HHmm = 0, HHmmss = 1, } #endregion 控件公有属性 调用端使用 #endregion 用户自定义属性 #region 用户自定义事件 /// /// 定义委托 /// /// /// public delegate void TimeChanged(object sender, EventArgs e); /// /// 定义事件 /// public event TimeChanged TimeValueChanged; #endregion 用户自定义事件 /// /// 用户自定义控件-时间控件 /// public TimeControl() { InitializeComponent(); } /// /// 窗体加载 /// /// /// private void TimeControl_Load(object sender, EventArgs e) { ChangeFormatString(); dateEdit.EditValue = TimeValue; } /// /// 显示格式改变后 /// private void ChangeFormatString() { #region 时间显示格式 //设置Mask.EditMask和DisplayFormat,EditFormat属性。设置为一致:TimeToString; //依照想要的显示格式设置此字符串。 this.dateEdit.Properties.DisplayFormat.FormatString = FormatString; this.dateEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime; this.dateEdit.Properties.EditFormat.FormatString = FormatString; this.dateEdit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime; this.dateEdit.Properties.Mask.EditMask = FormatString; //假设要显示时间须要设置VistaDisplayMode=true, VistaEditTime=true。意思为显示时间。在日期控件中会出现时钟图标,并能够编辑时间。 this.dateEdit.Properties.VistaDisplayMode = defaultBoolean; this.dateEdit.Properties.VistaEditTime = defaultBoolean; //设置时间部分的格式,时间部分指的是打开日期控件后,在时钟图标下的显示的日期格式。 this.dateEdit.Properties.VistaTimeProperties.DisplayFormat.FormatString = EditMask; this.dateEdit.Properties.VistaTimeProperties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime; this.dateEdit.Properties.VistaTimeProperties.EditFormat.FormatString = EditMask; this.dateEdit.Properties.VistaTimeProperties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime; this.dateEdit.Properties.VistaTimeProperties.Mask.EditMask = EditMask; //年月时仅显示月份 if (FormatString == "yyyy年MM月") { this.dateEdit.Properties.VistaCalendarInitialViewStyle = DevExpress.XtraEditors.VistaCalendarInitialViewStyle.YearView; this.dateEdit.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView; } if (FormatString == "yyyy") { this.dateEdit.Properties.VistaCalendarInitialViewStyle = DevExpress.XtraEditors.VistaCalendarInitialViewStyle.YearView; this.dateEdit.Properties.VistaCalendarViewStyle = DevExpress.XtraEditors.VistaCalendarViewStyle.YearView; } #endregion 时间显示格式 } /// /// 控件关闭事件 /// /// /// private void DateEdit_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e) { try { if (PublicClass.ToString(dateEdit.EditValue, "") == "") { dateEdit.EditValue = DateTime.Now.ToString(FormatString); } } catch (Exception ex) { PublicClass.WriteErrorLog("时间用户控件", "给指定时间控件为空时赋值当前时间:\r\n" + ex); } } /// /// 控件点击事件 /// /// /// private void DateEdit_Click(object sender, EventArgs e) { try { if (PublicClass.ToString(dateEdit.EditValue, "") == "") { dateEdit.EditValue = DateTime.Now.ToString(FormatString); } else { dateEdit_EditValueChanged(sender, e); } } catch (Exception ex) { PublicClass.WriteErrorLog("时间用户控件", "给指定时间控件为空时赋值当前时间:\r\n" + ex); } } /// /// 时间控件更改事件 /// /// /// private void dateEdit_EditValueChanged(object sender, EventArgs e) { //if (TimeValueChanged != null) // TimeValueChanged(sender, new EventArgs());//把控件自身作为参数传递 TimeValueChanged?.Invoke(sender, new EventArgs());//把控件自身作为参数传递 } /// /// 更改控件颜色 /// /// /// private void TimeControl_ForeColorChanged(object sender, EventArgs e) { dateEdit.ForeColor = ForeColor; } } }