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