using System;
using System.ComponentModel;
using System.Drawing;
namespace Dongke.WinForm.Controls
{
///
/// 时间范围设定
///
[ToolboxBitmap(typeof(DtrDateTimeRange), "ToolboxBitmap.DateTimeRange_00.bmp")]
public partial class DtrTimeRange : Component
{
#region 成员变量
///
/// 开始时间控件
///
private ITimeRange _beginDateTime = null;
///
/// 结束时间控件
///
private ITimeRange _endDateTime = null;
///
/// 时间间隔单位
///
private TimeRangeUnit _rangeUnit = TimeRangeUnit.Hour;
///
/// 最大时间间隔
///
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 DtrTimeRange()
{
}
///
/// 时间范围设定
///
///
public DtrTimeRange(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 ITimeRange 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 ITimeRange 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(TimeRangeUnit), "Hour")]
public TimeRangeUnit 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;
}
TimeSpan max = this._endDateTime.Value.Value;
if (this._beginDateTime.Value > max)
{
this._beginDateTime.Value = max;
return;
}
TimeSpan min = this.GetTimeSpan(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;
}
TimeSpan min = this._beginDateTime.Value.Value;
if (this._endDateTime.Value < min)
{
this._endDateTime.Value = min;
return;
}
TimeSpan max = this.GetTimeSpan(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;
}
TimeSpan min = this._beginDateTime.MinTime;
TimeSpan max = this.GetTimeSpan(this._beginDateTime.MaxTime, this._maxTimeSpan, this._rangeUnit);
if (this._endDateTime.MinTime < min)
{
this._endDateTime.MinTime = min;
}
if (this._endDateTime.MaxTime > max)
{
this._endDateTime.MaxTime = max;
}
min = this.GetTimeSpan(this._endDateTime.MinTime, -this._maxTimeSpan, this._rangeUnit);
max = this._endDateTime.MaxTime;
if (this._beginDateTime.MinTime < min)
{
this._beginDateTime.MinTime = min;
}
if (this._beginDateTime.MaxTime > max)
{
this._beginDateTime.MaxTime = max;
}
if (this._endDateTime.Value < this._beginDateTime.Value)
{
this._endDateTime.Value = this._beginDateTime.Value;
}
this._isSetting = false;
}
finally
{
this._isSetting = false;
}
}
///
/// 获取时间
///
/// 时间
/// 间隔
/// 间隔单位
/// 新的时间
protected virtual TimeSpan GetTimeSpan(TimeSpan dt, int ts, TimeRangeUnit ru)
{
try
{
DateTime d = DateTime.Now.Date + dt;
DateTime d1;
switch (ru)
{
case TimeRangeUnit.Hour:
d1 = d.AddHours(ts);
break;
case TimeRangeUnit.Minute:
d1 = d.AddMinutes(ts);
break;
case TimeRangeUnit.Second:
d1 = d.AddSeconds(ts);
break;
case TimeRangeUnit.Millisecond:
d1 = d.AddMilliseconds(ts);
break;
default:
return ts > 0 ? Constant.TimeSpanEndDay : Constant.TimeSpanBeginDay;
}
if (d1.Date > d.Date)
{
return Constant.TimeSpanEndDay;
}
if (d1.Date < d.Date)
{
return Constant.TimeSpanBeginDay;
}
return d1.TimeOfDay;
}
catch
{
return ts > 0 ? Constant.TimeSpanEndDay : Constant.TimeSpanBeginDay;
}
}
#endregion
}
}