DtrTimeRange.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. 
  2. using System;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. namespace Dongke.WinForm.Controls
  6. {
  7. /// <summary>
  8. /// 时间范围设定
  9. /// </summary>
  10. [ToolboxBitmap(typeof(DtrDateTimeRange), "ToolboxBitmap.DateTimeRange_00.bmp")]
  11. public partial class DtrTimeRange : Component
  12. {
  13. #region 成员变量
  14. /// <summary>
  15. /// 开始时间控件
  16. /// </summary>
  17. private ITimeRange _beginDateTime = null;
  18. /// <summary>
  19. /// 结束时间控件
  20. /// </summary>
  21. private ITimeRange _endDateTime = null;
  22. /// <summary>
  23. /// 时间间隔单位
  24. /// </summary>
  25. private TimeRangeUnit _rangeUnit = TimeRangeUnit.Hour;
  26. /// <summary>
  27. /// 最大时间间隔
  28. /// </summary>
  29. private int _maxTimeSpan = 0;
  30. /// <summary>
  31. /// 是否自动验证时间
  32. /// </summary>
  33. private DateTimeRangeAutoCheck _autoCheck = DateTimeRangeAutoCheck.OnValidated;
  34. /// <summary>
  35. /// 正在设置时间
  36. /// </summary>
  37. private bool _isSetting = false;
  38. ///// <summary>
  39. ///// 正在设置开始时间
  40. ///// </summary>
  41. //private bool _isBeginSetting = false;
  42. ///// <summary>
  43. ///// 正在设置结束时间
  44. ///// </summary>
  45. //private bool _isEndSetting = false;
  46. private bool _notCheckRange = true;
  47. #endregion
  48. #region 构造函数
  49. /// <summary>
  50. /// 时间范围设定
  51. /// </summary>
  52. public DtrTimeRange()
  53. {
  54. }
  55. /// <summary>
  56. /// 时间范围设定
  57. /// </summary>
  58. /// <param name="container"></param>
  59. public DtrTimeRange(IContainer container)
  60. {
  61. container.Add(this);
  62. }
  63. #endregion
  64. #region 属性
  65. /// <summary>
  66. /// 获取或设置是否自动验证时间。
  67. /// </summary>
  68. [Description("获取或设置是否自动验证时间。"), Category("CustomerEx")]
  69. [DefaultValue(typeof(DateTimeRangeAutoCheck), "OnValidated")]
  70. public DateTimeRangeAutoCheck AutoCheck
  71. {
  72. get
  73. {
  74. return this._autoCheck;
  75. }
  76. set
  77. {
  78. if (this._autoCheck != value)
  79. {
  80. this._autoCheck = value;
  81. this.CheckRange();
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 获取或设置开始日期时间。
  87. /// </summary>
  88. [Description("获取或设置开始时间。"), Category("CustomerEx")]
  89. [DefaultValue(null), TypeConverter(typeof(SelectedDTPConverter<ITimeRange>))]
  90. public ITimeRange BeginDateTime
  91. {
  92. get
  93. {
  94. return this._beginDateTime;
  95. }
  96. set
  97. {
  98. if (this._beginDateTime != value)
  99. {
  100. if (this._endDateTime != null && this._endDateTime == value)
  101. {
  102. return;
  103. }
  104. if (this._beginDateTime != null)
  105. {
  106. this._beginDateTime.ValueChanged -= BeginDateTime_ValueChanged;
  107. this._beginDateTime.Validated -= BeginDateTime_Validated;
  108. }
  109. this._beginDateTime = value;
  110. if (this._beginDateTime != null)
  111. {
  112. this._beginDateTime.ValueChanged += BeginDateTime_ValueChanged;
  113. this._beginDateTime.Validated += BeginDateTime_Validated;
  114. }
  115. this.CheckRange();
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 获取或设置结束日期时间。
  121. /// </summary>
  122. [Description("获取或设置结束时间。"), Category("CustomerEx")]
  123. [DefaultValue(null), TypeConverter(typeof(SelectedDTPConverter<ITimeRange>))]
  124. public ITimeRange EndDateTime
  125. {
  126. get
  127. {
  128. return this._endDateTime;
  129. }
  130. set
  131. {
  132. if (this._endDateTime != value)
  133. {
  134. if (this._beginDateTime != null && this._beginDateTime == value)
  135. {
  136. return;
  137. }
  138. if (this._endDateTime != null)
  139. {
  140. this._endDateTime.ValueChanged -= EndDateTime_ValueChanged;
  141. this._endDateTime.Validated -= EndDateTime_Validated;
  142. }
  143. this._endDateTime = value;
  144. if (this._endDateTime != null)
  145. {
  146. this._endDateTime.ValueChanged += EndDateTime_ValueChanged;
  147. this._endDateTime.Validated += EndDateTime_Validated;
  148. }
  149. this.CheckRange();
  150. }
  151. }
  152. }
  153. ///// <summary>
  154. ///// 获取是否正在设置开始时间
  155. ///// </summary>
  156. //[Description("获取是否正在设置开始时间。"), Category("CustomerEx")]
  157. //[DefaultValue(false)]
  158. //public bool IsBeginSetting
  159. //{
  160. // get
  161. // {
  162. // return this._isBeginSetting;
  163. // }
  164. //}
  165. ///// <summary>
  166. ///// 获取是否正在设置结束时间
  167. ///// </summary>
  168. //[Description("获取是否正在设置结束时间。"), Category("CustomerEx")]
  169. //[DefaultValue(false)]
  170. //public bool IsEndSetting
  171. //{
  172. // get
  173. // {
  174. // return this._isEndSetting;
  175. // }
  176. //}
  177. /// <summary>
  178. /// 获取或设置最大时间间隔,0 无限制。
  179. /// </summary>
  180. [Description("获取或设置最大时间间隔,0 无限制。"), Category("CustomerEx")]
  181. [DefaultValue(0)]
  182. public int MaxTimeSpan
  183. {
  184. get
  185. {
  186. return this._maxTimeSpan;
  187. }
  188. set
  189. {
  190. if (value < 0)
  191. {
  192. value = 0;
  193. }
  194. if (this._maxTimeSpan != value)
  195. {
  196. this._maxTimeSpan = value;
  197. this.CheckRange();
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 获取或设置时间间隔单位。
  203. /// </summary>
  204. [Description("获取或设置时间间隔单位。"), Category("CustomerEx")]
  205. [DefaultValue(typeof(TimeRangeUnit), "Hour")]
  206. public TimeRangeUnit RangeUnit
  207. {
  208. get
  209. {
  210. return this._rangeUnit;
  211. }
  212. set
  213. {
  214. if (this._rangeUnit != value)
  215. {
  216. this._rangeUnit = value;
  217. this.CheckRange();
  218. }
  219. }
  220. }
  221. #endregion
  222. #region 事件
  223. /// <summary>
  224. /// 开始时间变更
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void BeginDateTime_ValueChanged(object sender, EventArgs e)
  229. {
  230. if (this._autoCheck == DateTimeRangeAutoCheck.OnValueChanged)
  231. {
  232. // 验证结束时间
  233. this.CheckEndDateTime();
  234. }
  235. }
  236. /// <summary>
  237. /// 结束时间变更
  238. /// </summary>
  239. /// <param name="sender"></param>
  240. /// <param name="e"></param>
  241. private void EndDateTime_ValueChanged(object sender, EventArgs e)
  242. {
  243. if (this._autoCheck == DateTimeRangeAutoCheck.OnValueChanged)
  244. {
  245. // 验证开始时间
  246. this.CheckBeginDateTime();
  247. }
  248. }
  249. /// <summary>
  250. /// 开始时间变更
  251. /// </summary>
  252. /// <param name="sender"></param>
  253. /// <param name="e"></param>
  254. private void BeginDateTime_Validated(object sender, EventArgs e)
  255. {
  256. if (this._autoCheck == DateTimeRangeAutoCheck.OnValidated)
  257. {
  258. // 验证结束时间
  259. this.CheckEndDateTime();
  260. }
  261. }
  262. /// <summary>
  263. /// 结束时间变更
  264. /// </summary>
  265. /// <param name="sender"></param>
  266. /// <param name="e"></param>
  267. private void EndDateTime_Validated(object sender, EventArgs e)
  268. {
  269. if (this._autoCheck == DateTimeRangeAutoCheck.OnValidated)
  270. {
  271. // 验证开始时间
  272. this.CheckBeginDateTime();
  273. }
  274. }
  275. #endregion
  276. #region 公共方法
  277. #endregion
  278. #region 保护方法
  279. /// <summary>
  280. /// 验证开始时间
  281. /// </summary>
  282. protected virtual void CheckBeginDateTime()
  283. {
  284. if (this._beginDateTime == null || this._beginDateTime.Value == null ||
  285. this._endDateTime == null || this._endDateTime.Value == null ||
  286. this._isSetting)
  287. {
  288. return;
  289. }
  290. try
  291. {
  292. this._isSetting = true;
  293. //this._isBeginSetting = true;
  294. if (this._maxTimeSpan == 0)
  295. {
  296. if (this._endDateTime.Value < this._beginDateTime.Value)
  297. {
  298. this._beginDateTime.Value = this._endDateTime.Value;
  299. }
  300. return;
  301. }
  302. TimeSpan max = this._endDateTime.Value.Value;
  303. if (this._beginDateTime.Value > max)
  304. {
  305. this._beginDateTime.Value = max;
  306. return;
  307. }
  308. TimeSpan min = this.GetTimeSpan(this._endDateTime.Value.Value, -this._maxTimeSpan, this._rangeUnit);
  309. if (this._beginDateTime.Value < min)
  310. {
  311. this._beginDateTime.Value = min;
  312. return;
  313. }
  314. }
  315. finally
  316. {
  317. this._isSetting = false;
  318. //this._isBeginSetting = false;
  319. }
  320. }
  321. /// <summary>
  322. /// 验证结束时间
  323. /// </summary>
  324. protected virtual void CheckEndDateTime()
  325. {
  326. if (this._beginDateTime == null || this._beginDateTime.Value == null ||
  327. this._endDateTime == null || this._endDateTime.Value == null ||
  328. this._isSetting)
  329. {
  330. return;
  331. }
  332. try
  333. {
  334. this._isSetting = true;
  335. //this._isEndSetting = true;
  336. if (this._maxTimeSpan == 0)
  337. {
  338. if (this._endDateTime.Value < this._beginDateTime.Value)
  339. {
  340. this._endDateTime.Value = this._beginDateTime.Value;
  341. }
  342. return;
  343. }
  344. TimeSpan min = this._beginDateTime.Value.Value;
  345. if (this._endDateTime.Value < min)
  346. {
  347. this._endDateTime.Value = min;
  348. return;
  349. }
  350. TimeSpan max = this.GetTimeSpan(this._beginDateTime.Value.Value, this._maxTimeSpan, this._rangeUnit);
  351. if (this._endDateTime.Value > max)
  352. {
  353. this._endDateTime.Value = max;
  354. return;
  355. }
  356. }
  357. finally
  358. {
  359. this._isSetting = false;
  360. //this._isEndSetting = false;
  361. }
  362. }
  363. /// <summary>
  364. /// 验证开始、结束时间
  365. /// </summary>
  366. protected virtual void CheckRange()
  367. {
  368. if (this._notCheckRange)
  369. {
  370. return;
  371. }
  372. if (this._autoCheck == DateTimeRangeAutoCheck.Disable ||
  373. this._beginDateTime == null || this._beginDateTime.Value == null ||
  374. this._endDateTime == null || this._endDateTime.Value == null ||
  375. this._isSetting)
  376. {
  377. return;
  378. }
  379. try
  380. {
  381. this._isSetting = true;
  382. if (this._maxTimeSpan == 0)
  383. {
  384. if (this._endDateTime.Value < this._beginDateTime.Value)
  385. {
  386. this._endDateTime.Value = this._beginDateTime.Value;
  387. }
  388. return;
  389. }
  390. TimeSpan min = this._beginDateTime.MinTime;
  391. TimeSpan max = this.GetTimeSpan(this._beginDateTime.MaxTime, this._maxTimeSpan, this._rangeUnit);
  392. if (this._endDateTime.MinTime < min)
  393. {
  394. this._endDateTime.MinTime = min;
  395. }
  396. if (this._endDateTime.MaxTime > max)
  397. {
  398. this._endDateTime.MaxTime = max;
  399. }
  400. min = this.GetTimeSpan(this._endDateTime.MinTime, -this._maxTimeSpan, this._rangeUnit);
  401. max = this._endDateTime.MaxTime;
  402. if (this._beginDateTime.MinTime < min)
  403. {
  404. this._beginDateTime.MinTime = min;
  405. }
  406. if (this._beginDateTime.MaxTime > max)
  407. {
  408. this._beginDateTime.MaxTime = max;
  409. }
  410. if (this._endDateTime.Value < this._beginDateTime.Value)
  411. {
  412. this._endDateTime.Value = this._beginDateTime.Value;
  413. }
  414. this._isSetting = false;
  415. }
  416. finally
  417. {
  418. this._isSetting = false;
  419. }
  420. }
  421. /// <summary>
  422. /// 获取时间
  423. /// </summary>
  424. /// <param name="dt">时间</param>
  425. /// <param name="ts">间隔</param>
  426. /// <param name="ru">间隔单位</param>
  427. /// <returns>新的时间</returns>
  428. protected virtual TimeSpan GetTimeSpan(TimeSpan dt, int ts, TimeRangeUnit ru)
  429. {
  430. try
  431. {
  432. DateTime d = DateTime.Now.Date + dt;
  433. DateTime d1;
  434. switch (ru)
  435. {
  436. case TimeRangeUnit.Hour:
  437. d1 = d.AddHours(ts);
  438. break;
  439. case TimeRangeUnit.Minute:
  440. d1 = d.AddMinutes(ts);
  441. break;
  442. case TimeRangeUnit.Second:
  443. d1 = d.AddSeconds(ts);
  444. break;
  445. case TimeRangeUnit.Millisecond:
  446. d1 = d.AddMilliseconds(ts);
  447. break;
  448. default:
  449. return ts > 0 ? Constant.TimeSpanEndDay : Constant.TimeSpanBeginDay;
  450. }
  451. if (d1.Date > d.Date)
  452. {
  453. return Constant.TimeSpanEndDay;
  454. }
  455. if (d1.Date < d.Date)
  456. {
  457. return Constant.TimeSpanBeginDay;
  458. }
  459. return d1.TimeOfDay;
  460. }
  461. catch
  462. {
  463. return ts > 0 ? Constant.TimeSpanEndDay : Constant.TimeSpanBeginDay;
  464. }
  465. }
  466. #endregion
  467. }
  468. }