UCDateTimeSelectPan.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCDateTimeSelectPan.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  8. // </copyright>
  9. //
  10. // Blog: https://www.cnblogs.com/bfyx
  11. // GitHub:https://github.com/kwwwvagaa/NetWinformControl
  12. // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
  13. //
  14. // If you use this code, please keep this note.
  15. // ***********************************************************************
  16. using System;
  17. using System.Collections.Generic;
  18. using System.ComponentModel;
  19. using System.Drawing;
  20. using System.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCDateTimeSelectPan.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. [ToolboxItem(false)]
  32. public partial class UCDateTimeSelectPan : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [selected time event].
  36. /// </summary>
  37. [Description("确定事件"), Category("自定义")]
  38. public event EventHandler SelectedTimeEvent;
  39. /// <summary>
  40. /// Occurs when [cancel time event].
  41. /// </summary>
  42. [Description("取消事件"), Category("自定义")]
  43. public event EventHandler CancelTimeEvent;
  44. /// <summary>
  45. /// The automatic select next
  46. /// </summary>
  47. private bool autoSelectNext = true;
  48. /// <summary>
  49. /// Gets or sets a value indicating whether [automatic select next].
  50. /// </summary>
  51. /// <value><c>true</c> if [automatic select next]; otherwise, <c>false</c>.</value>
  52. [Description("自动选中下一级"), Category("自定义")]
  53. public bool AutoSelectNext
  54. {
  55. get { return autoSelectNext; }
  56. set { autoSelectNext = value; }
  57. }
  58. /// <summary>
  59. /// The m dt
  60. /// </summary>
  61. DateTime m_dt = DateTime.Now;
  62. /// <summary>
  63. /// Gets or sets the current time.
  64. /// </summary>
  65. /// <value>The current time.</value>
  66. public DateTime CurrentTime
  67. {
  68. get { return m_dt; }
  69. set
  70. {
  71. m_dt = value;
  72. SetTimeToControl();
  73. }
  74. }
  75. /// <summary>
  76. /// The m this BTN
  77. /// </summary>
  78. UCBtnExt m_thisBtn = null;
  79. /// <summary>
  80. /// The m type
  81. /// </summary>
  82. DateTimePickerType m_type = DateTimePickerType.DateTime;
  83. /// <summary>
  84. /// Gets or sets the type of the time.
  85. /// </summary>
  86. /// <value>The type of the time.</value>
  87. [Description("时间类型"), Category("自定义")]
  88. public DateTimePickerType TimeType
  89. {
  90. get { return m_type; }
  91. set { m_type = value; }
  92. }
  93. /// <summary>
  94. /// Initializes a new instance of the <see cref="UCDateTimeSelectPan" /> class.
  95. /// </summary>
  96. public UCDateTimeSelectPan()
  97. {
  98. InitializeComponent();
  99. panTime.SelectSourceEvent += panTime_SelectSourceEvent;
  100. this.TabStop = false;
  101. }
  102. /// <summary>
  103. /// Initializes a new instance of the <see cref="UCDateTimeSelectPan" /> class.
  104. /// </summary>
  105. /// <param name="dt">The dt.</param>
  106. public UCDateTimeSelectPan(DateTime dt)
  107. {
  108. m_dt = dt;
  109. InitializeComponent();
  110. panTime.SelectSourceEvent += panTime_SelectSourceEvent;
  111. this.TabStop = false;
  112. }
  113. /// <summary>
  114. /// Handles the SelectSourceEvent event of the panTime control.
  115. /// </summary>
  116. /// <param name="sender">The source of the event.</param>
  117. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  118. void panTime_SelectSourceEvent(object sender, EventArgs e)
  119. {
  120. string strKey = sender.ToString();
  121. if (m_thisBtn == btnYear)
  122. {
  123. m_dt = (strKey + "-" + m_dt.Month + "-" + m_dt.Day + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
  124. }
  125. else if (m_thisBtn == btnMonth)
  126. {
  127. m_dt = (m_dt.Year + "-" + strKey + "-" + m_dt.Day + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
  128. }
  129. else if (m_thisBtn == btnDay)
  130. {
  131. m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + strKey + " " + m_dt.Hour + ":" + m_dt.Minute).ToDate();
  132. }
  133. else if (m_thisBtn == btnHour)
  134. {
  135. m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + m_dt.Day + " " + strKey + ":" + m_dt.Minute).ToDate();
  136. }
  137. else if (m_thisBtn == btnMinute)
  138. {
  139. m_dt = (m_dt.Year + "-" + m_dt.Month + "-" + m_dt.Day + " " + m_dt.Hour + ":" + strKey).ToDate();
  140. }
  141. SetTimeToControl();
  142. if (this.Visible)
  143. {
  144. if (autoSelectNext)
  145. {
  146. if (m_thisBtn == btnYear)
  147. {
  148. SetSelectType(btnMonth);
  149. }
  150. else if (m_thisBtn == btnMonth)
  151. {
  152. SetSelectType(btnDay);
  153. }
  154. else if (m_thisBtn == btnDay)
  155. {
  156. if (m_type == DateTimePickerType.DateTime || m_type == DateTimePickerType.Time)
  157. SetSelectType(btnHour);
  158. }
  159. else if (m_thisBtn == btnHour)
  160. {
  161. SetSelectType(btnMinute);
  162. }
  163. }
  164. }
  165. }
  166. /// <summary>
  167. /// Handles the Load event of the UCDateTimePickerExt control.
  168. /// </summary>
  169. /// <param name="sender">The source of the event.</param>
  170. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  171. private void UCDateTimePickerExt_Load(object sender, EventArgs e)
  172. {
  173. SetTimeToControl();
  174. if (m_type == DateTimePickerType.Date)
  175. {
  176. btnHour.Visible = false;
  177. btnMinute.Visible = false;
  178. }
  179. else if (m_type == DateTimePickerType.Time)
  180. {
  181. btnYear.Visible = false;
  182. btnMonth.Visible = false;
  183. btnDay.Visible = false;
  184. sp1.Visible = false;
  185. sp2.Visible = false;
  186. sp3.Visible = false;
  187. }
  188. if ((int)m_type <= 2)
  189. {
  190. SetSelectType(btnYear);
  191. }
  192. else
  193. {
  194. SetSelectType(btnHour);
  195. }
  196. }
  197. /// <summary>
  198. /// Sets the time to control.
  199. /// </summary>
  200. private void SetTimeToControl()
  201. {
  202. btnYear.Tag = m_dt.Year;
  203. btnYear.BtnText = m_dt.Year + "年";
  204. btnMonth.Tag = m_dt.Month;
  205. btnMonth.BtnText = m_dt.Month.ToString().PadLeft(2, '0') + "月";
  206. btnDay.Tag = m_dt.Day;
  207. btnDay.BtnText = m_dt.Day.ToString().PadLeft(2, '0') + "日";
  208. btnHour.Tag = m_dt.Hour;
  209. btnHour.BtnText = m_dt.Hour.ToString().PadLeft(2, '0') + "时";
  210. btnMinute.Tag = m_dt.Minute;
  211. btnMinute.BtnText = m_dt.Minute.ToString().PadLeft(2, '0') + "分";
  212. }
  213. /// <summary>
  214. /// Sets the type of the select.
  215. /// </summary>
  216. /// <param name="btn">The BTN.</param>
  217. private void SetSelectType(UCBtnExt btn)
  218. {
  219. try
  220. {
  221. ControlHelper.FreezeControl(this, true);
  222. if (m_thisBtn != null)
  223. {
  224. m_thisBtn.FillColor = Color.White;
  225. m_thisBtn.BtnForeColor = Color.FromArgb(255, 77, 59);
  226. }
  227. m_thisBtn = btn;
  228. if (m_thisBtn != null)
  229. {
  230. m_thisBtn.FillColor = Color.FromArgb(255, 77, 59);
  231. m_thisBtn.BtnForeColor = Color.White;
  232. List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
  233. panTime.SuspendLayout();
  234. if (btn == btnYear)
  235. {
  236. panLeft.Visible = true;
  237. panRight.Visible = true;
  238. }
  239. else
  240. {
  241. panLeft.Visible = false;
  242. panRight.Visible = false;
  243. }
  244. if (btn == btnYear)
  245. {
  246. panTime.Row = 5;
  247. panTime.Column = 6;
  248. int intYear = m_dt.Year - m_dt.Year % 30;
  249. for (int i = 0; i < 30; i++)
  250. {
  251. lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
  252. }
  253. }
  254. else if (btn == btnMonth)
  255. {
  256. panTime.Row = 3;
  257. panTime.Column = 4;
  258. for (int i = 1; i <= 12; i++)
  259. {
  260. lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(2, '0') + "月\r\n" + (("2019-" + i + "-01").ToDate().ToString("MMM", System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")))));
  261. }
  262. }
  263. else if (btn == btnDay)
  264. {
  265. panTime.Column = 7;
  266. int intDayCount = DateTime.DaysInMonth(m_dt.Year, m_dt.Month);
  267. int intIndex = (int)(m_dt.DayOfWeek);
  268. panTime.Row = (intDayCount + intIndex) / 7 + ((intDayCount + intIndex) % 7 != 0 ? 1 : 0);
  269. for (int i = 0; i < intIndex; i++)
  270. {
  271. lstSource.Add(new KeyValuePair<string, string>("", ""));
  272. }
  273. for (int i = 1; i <= intDayCount; i++)
  274. {
  275. lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(2, '0')));
  276. }
  277. }
  278. else if (btn == btnHour)
  279. {
  280. panTime.Row = 4;
  281. panTime.Column = 6;
  282. for (int i = 0; i <= 24; i++)
  283. {
  284. lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString() + "时"));
  285. }
  286. }
  287. else if (btn == btnMinute)
  288. {
  289. panTime.Row = 5;
  290. panTime.Column = 12;
  291. for (int i = 0; i <= 60; i++)
  292. {
  293. lstSource.Add(new KeyValuePair<string, string>(i.ToString(), i.ToString().PadLeft(2, '0')));
  294. }
  295. }
  296. panTime.Source = lstSource;
  297. panTime.SetSelect(btn.Tag.ToStringExt());
  298. panTime.ResumeLayout(true);
  299. // panTime.Enabled = true;
  300. }
  301. }
  302. finally
  303. {
  304. ControlHelper.FreezeControl(this, false);
  305. }
  306. }
  307. /// <summary>
  308. /// Handles the BtnClick event of the btnTime control.
  309. /// </summary>
  310. /// <param name="sender">The source of the event.</param>
  311. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  312. private void btnTime_BtnClick(object sender, EventArgs e)
  313. {
  314. SetSelectType((UCBtnExt)sender);
  315. }
  316. /// <summary>
  317. /// Handles the MouseDown event of the panLeft control.
  318. /// </summary>
  319. /// <param name="sender">The source of the event.</param>
  320. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  321. private void panLeft_MouseDown(object sender, MouseEventArgs e)
  322. {
  323. List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
  324. int intYear = this.panTime.Source[0].Key.ToInt() - this.panTime.Source[0].Key.ToInt() % 30 - 30;
  325. panTime.SuspendLayout();
  326. panTime.Row = 5;
  327. panTime.Column = 6;
  328. for (int i = 0; i < 30; i++)
  329. {
  330. lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
  331. }
  332. panTime.Source = lstSource;
  333. panTime.SetSelect(btnYear.Tag.ToStringExt());
  334. panTime.ResumeLayout(true);
  335. }
  336. /// <summary>
  337. /// Handles the MouseDown event of the panRight control.
  338. /// </summary>
  339. /// <param name="sender">The source of the event.</param>
  340. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  341. private void panRight_MouseDown(object sender, MouseEventArgs e)
  342. {
  343. List<KeyValuePair<string, string>> lstSource = new List<KeyValuePair<string, string>>();
  344. int intYear = this.panTime.Source[0].Key.ToInt() - this.panTime.Source[0].Key.ToInt() % 30 + 30;
  345. panTime.SuspendLayout();
  346. panTime.Row = 5;
  347. panTime.Column = 6;
  348. for (int i = 0; i < 30; i++)
  349. {
  350. lstSource.Add(new KeyValuePair<string, string>((intYear + i).ToString(), (intYear + i).ToString()));
  351. }
  352. panTime.Source = lstSource;
  353. panTime.SetSelect(btnYear.Tag.ToStringExt());
  354. panTime.ResumeLayout(true);
  355. }
  356. /// <summary>
  357. /// Handles the BtnClick event of the btnOk control.
  358. /// </summary>
  359. /// <param name="sender">The source of the event.</param>
  360. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  361. private void btnOk_BtnClick(object sender, EventArgs e)
  362. {
  363. if (SelectedTimeEvent != null)
  364. SelectedTimeEvent(m_dt, null);
  365. }
  366. /// <summary>
  367. /// Handles the BtnClick event of the btnCancel control.
  368. /// </summary>
  369. /// <param name="sender">The source of the event.</param>
  370. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  371. private void btnCancel_BtnClick(object sender, EventArgs e)
  372. {
  373. if (CancelTimeEvent != null)
  374. {
  375. CancelTimeEvent(null, null);
  376. }
  377. }
  378. }
  379. }