using System; using System.ComponentModel; using System.Drawing; namespace Dongke.WinForm.Controls { /// /// 日期/时间范围设定 /// [ToolboxBitmap(typeof(DtrDateTimeRange), "ToolboxBitmap.DateTimeRange_00.bmp")] public partial class DtrDateTimeRange : Component { #region 成员变量 /// /// 开始日期/时间控件 /// private IDateTimeRange _beginDateTime = null; /// /// 结束日期/时间控件 /// private IDateTimeRange _endDateTime = null; /// /// 日期/时间间隔单位 /// private DateTimeRangeUnit _rangeUnit = DateTimeRangeUnit.Year; /// /// 最大日期/时间间隔 /// private int _maxTimeSpan = 0; /// /// 是否自动验证日期/时间 /// private DateTimeRangeAutoCheck _autoCheck = DateTimeRangeAutoCheck.OnValidated; /// /// 正在设置日期/时间 /// private bool _isSetting = false; ///// ///// 正在设置开始日期/时间 ///// //private bool _isBeginSetting = false; ///// ///// 正在设置结束日期/时间 ///// //private bool _isEndSetting = false; private bool _notCheckRange = true; #endregion #region 构造函数 /// /// 日日期/时间范围设定 /// public DtrDateTimeRange() { } /// /// 日期/时间范围设定 /// /// public DtrDateTimeRange(IContainer container) { container.Add(this); } #endregion #region 属性 /// /// 获取或设置是否自动验证日期/时间。 /// [Description("获取或设置是否自动验证日期/时间。"), Category("CustomerEx")] [DefaultValue(typeof(DateTimeRangeAutoCheck), "OnValidated")] public DateTimeRangeAutoCheck AutoCheck { get { return this._autoCheck; } set { if (this._autoCheck != value) { this._autoCheck = value; this.CheckRange(); } } } /// /// 获取或设置开始日期时间。 /// [Description("获取或设置开始日期/时间。"), Category("CustomerEx")] [DefaultValue(null), TypeConverter(typeof(SelectedDTPConverter))] public IDateTimeRange BeginDateTime { get { return this._beginDateTime; } set { if (this._beginDateTime != value) { if (this._endDateTime != null && this._endDateTime == value) { return; } if (this._beginDateTime != null) { this._beginDateTime.ValueChanged -= BeginDateTime_ValueChanged; this._beginDateTime.Validated -= BeginDateTime_Validated; } this._beginDateTime = value; if (this._beginDateTime != null) { this._beginDateTime.ValueChanged += BeginDateTime_ValueChanged; this._beginDateTime.Validated += BeginDateTime_Validated; } this.CheckRange(); } } } /// /// 获取或设置结束日期时间。 /// [Description("获取或设置结束日期/时间。"), Category("CustomerEx")] [DefaultValue(null), TypeConverter(typeof(SelectedDTPConverter))] public IDateTimeRange EndDateTime { get { return this._endDateTime; } set { if (this._endDateTime != value) { if (this._beginDateTime != null && this._beginDateTime == value) { return; } if (this._endDateTime != null) { this._endDateTime.ValueChanged -= EndDateTime_ValueChanged; this._endDateTime.Validated -= EndDateTime_Validated; } this._endDateTime = value; if (this._endDateTime != null) { this._endDateTime.ValueChanged += EndDateTime_ValueChanged; this._endDateTime.Validated += EndDateTime_Validated; } this.CheckRange(); } } } ///// ///// 获取是否正在设置开始日期/时间 ///// //[Description("获取是否正在设置开始日期/时间。"), Category("CustomerEx")] //[DefaultValue(false)] //public bool IsBeginSetting //{ // get // { // return this._isBeginSetting; // } //} ///// ///// 获取是否正在设置结束日期/时间 ///// //[Description("获取是否正在设置结束日期/时间。"), Category("CustomerEx")] //[DefaultValue(false)] //public bool IsEndSetting //{ // get // { // return this._isEndSetting; // } //} /// /// 获取或设置最大日期/时间间隔,0 无限制。 /// [Description("获取或设置最大日期/时间间隔,0 无限制。"), Category("CustomerEx")] [DefaultValue(0)] public int MaxTimeSpan { get { return this._maxTimeSpan; } set { if (value < 0) { value = 0; } if (this._maxTimeSpan != value) { this._maxTimeSpan = value; this.CheckRange(); } } } /// /// 获取或设置日期/时间间隔单位。 /// [Description("获取或设置日期/时间间隔单位。"), Category("CustomerEx")] [DefaultValue(typeof(DateTimeRangeUnit), "Year")] public DateTimeRangeUnit RangeUnit { get { return this._rangeUnit; } set { if (this._rangeUnit != value) { this._rangeUnit = value; this.CheckRange(); } } } #endregion #region 事件 /// /// 开始日期/时间变更 /// /// /// private void BeginDateTime_ValueChanged(object sender, EventArgs e) { if (this._autoCheck == DateTimeRangeAutoCheck.OnValueChanged) { // 验证结束日期/时间 this.CheckEndDateTime(); } } /// /// 结束日期/时间变更 /// /// /// private void EndDateTime_ValueChanged(object sender, EventArgs e) { if (this._autoCheck == DateTimeRangeAutoCheck.OnValueChanged) { // 验证开始日期/时间 this.CheckBeginDateTime(); } } /// /// 开始日期/时间变更 /// /// /// private void BeginDateTime_Validated(object sender, EventArgs e) { if (this._autoCheck == DateTimeRangeAutoCheck.OnValidated) { // 验证结束日期/时间 this.CheckEndDateTime(); } } /// /// 结束日期/时间变更 /// /// /// private void EndDateTime_Validated(object sender, EventArgs e) { if (this._autoCheck == DateTimeRangeAutoCheck.OnValidated) { // 验证开始日期/时间 this.CheckBeginDateTime(); } } #endregion #region 公共方法 #endregion #region 保护方法 /// /// 验证开始日期/时间 /// protected virtual void CheckBeginDateTime() { if (this._beginDateTime == null || this._beginDateTime.Value == null || this._endDateTime == null || this._endDateTime.Value == null || this._isSetting) { return; } try { this._isSetting = true; //this._isBeginSetting = true; if (this._maxTimeSpan == 0) { if (this._endDateTime.Value < this._beginDateTime.Value) { this._beginDateTime.Value = this._endDateTime.Value; } return; } DateTime max = this._endDateTime.Value.Value; if (this._beginDateTime.Value > max) { this._beginDateTime.Value = max; return; } DateTime min = this.GetDateTime(this._endDateTime.Value.Value, -this._maxTimeSpan, this._rangeUnit); if (this._beginDateTime.Value < min) { this._beginDateTime.Value = min; return; } } finally { this._isSetting = false; //this._isBeginSetting = false; } } /// /// 验证结束日期/时间 /// protected virtual void CheckEndDateTime() { if (this._beginDateTime == null || this._beginDateTime.Value == null || this._endDateTime == null || this._endDateTime.Value == null || this._isSetting) { return; } try { this._isSetting = true; //this._isEndSetting = true; if (this._maxTimeSpan == 0) { if (this._endDateTime.Value < this._beginDateTime.Value) { this._endDateTime.Value = this._beginDateTime.Value; } return; } DateTime min = this._beginDateTime.Value.Value; if (this._endDateTime.Value < min) { this._endDateTime.Value = min; return; } DateTime max = this.GetDateTime(this._beginDateTime.Value.Value, this._maxTimeSpan, this._rangeUnit); if (this._endDateTime.Value > max) { this._endDateTime.Value = max; return; } } finally { this._isSetting = false; //this._isEndSetting = false; } } /// /// 验证开始、结束日期/时间 /// protected virtual void CheckRange() { if (this._notCheckRange) { return; } if (this._autoCheck == DateTimeRangeAutoCheck.Disable || this._beginDateTime == null || this._beginDateTime.Value == null || this._endDateTime == null || this._endDateTime.Value == null || this._isSetting) { return; } try { this._isSetting = true; if (this._maxTimeSpan == 0) { if (this._endDateTime.Value < this._beginDateTime.Value) { this._endDateTime.Value = this._beginDateTime.Value; } return; } DateTime min = this._beginDateTime.MinDate; DateTime max = this.GetDateTime(this._beginDateTime.MaxDate, this._maxTimeSpan, this._rangeUnit); if (this._endDateTime.MinDate < min) { this._endDateTime.MinDate = min; } if (this._endDateTime.MaxDate > max) { this._endDateTime.MaxDate = max; } min = this.GetDateTime(this._endDateTime.MinDate, -this._maxTimeSpan, this._rangeUnit); max = this._endDateTime.MaxDate; if (this._beginDateTime.MinDate < min) { this._beginDateTime.MinDate = min; } if (this._beginDateTime.MaxDate > max) { this._beginDateTime.MaxDate = max; } if (this._endDateTime.Value < this._beginDateTime.Value) { this._endDateTime.Value = this._beginDateTime.Value; } this._isSetting = false; } finally { this._isSetting = false; } } /// /// 获取日期/时间 /// /// 日期/时间 /// 间隔 /// 间隔单位 /// 新的日期/时间 protected virtual DateTime GetDateTime(DateTime dt, int ts, DateTimeRangeUnit ru) { try { switch (ru) { case DateTimeRangeUnit.Year: return dt.AddYears(ts); case DateTimeRangeUnit.Month: return dt.AddMonths(ts); case DateTimeRangeUnit.Day: return dt.AddDays(ts); case DateTimeRangeUnit.Hour: return dt.AddHours(ts); case DateTimeRangeUnit.Minute: return dt.AddMinutes(ts); case DateTimeRangeUnit.Second: return dt.AddSeconds(ts); case DateTimeRangeUnit.Millisecond: return dt.AddMilliseconds(ts); default: return ts > 0 ? DateTime.MaxValue : DateTime.MinValue; } } catch { return ts > 0 ? DateTime.MaxValue : DateTime.MinValue; } } #endregion } }