UCDatePickerExt.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-08-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCDatePickerExt.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 UCDatePickerExt.
  28. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  29. /// </summary>
  30. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  31. public partial class UCDatePickerExt : UCControlBase
  32. {
  33. /// <summary>
  34. /// The m FRM anchor
  35. /// </summary>
  36. Forms.FrmAnchor m_frmAnchor;
  37. /// <summary>
  38. /// The m select pan
  39. /// </summary>
  40. UCDateTimeSelectPan m_selectPan = null;
  41. /// <summary>
  42. /// The m type
  43. /// </summary>
  44. DateTimePickerType m_type = DateTimePickerType.DateTime;
  45. /// <summary>
  46. /// Gets or sets the type of the time.
  47. /// </summary>
  48. /// <value>The type of the time.</value>
  49. [Description("时间类型"), Category("自定义")]
  50. public DateTimePickerType TimeType
  51. {
  52. get { return m_type; }
  53. set
  54. {
  55. m_type = value;
  56. if (value == DateTimePickerType.DateTime)
  57. {
  58. txtYear.Visible = true;
  59. label1.Visible = true;
  60. txtMonth.Visible = true;
  61. label2.Visible = true;
  62. txtDay.Visible = true;
  63. label3.Visible = true;
  64. txtHour.Visible = true;
  65. label4.Visible = true;
  66. txtMinute.Visible = true;
  67. label5.Visible = true;
  68. }
  69. else if (value == DateTimePickerType.Date)
  70. {
  71. txtYear.Visible = true;
  72. label1.Visible = true;
  73. txtMonth.Visible = true;
  74. label2.Visible = true;
  75. txtDay.Visible = true;
  76. label3.Visible = true;
  77. txtHour.Visible = false;
  78. label4.Visible = false;
  79. txtMinute.Visible = false;
  80. label5.Visible = false;
  81. }
  82. else
  83. {
  84. txtYear.Visible = false;
  85. label1.Visible = false;
  86. txtMonth.Visible = false;
  87. label2.Visible = false;
  88. txtDay.Visible = false;
  89. label3.Visible = false;
  90. txtHour.Visible = true;
  91. label4.Visible = true;
  92. txtMinute.Visible = true;
  93. label5.Visible = true;
  94. }
  95. }
  96. }
  97. /// <summary>
  98. /// The current time
  99. /// </summary>
  100. private DateTime currentTime = DateTime.Now;
  101. /// <summary>
  102. /// The time font size
  103. /// </summary>
  104. private int timeFontSize = 20;
  105. /// <summary>
  106. /// Gets or sets the size of the time font.
  107. /// </summary>
  108. /// <value>The size of the time font.</value>
  109. [Description("时间字体大小"), Category("自定义")]
  110. public int TimeFontSize
  111. {
  112. get { return timeFontSize; }
  113. set
  114. {
  115. if (timeFontSize != value)
  116. {
  117. timeFontSize = value;
  118. foreach (Control c in panel1.Controls)
  119. {
  120. c.Font = new Font(c.Font.Name, value);
  121. }
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// Gets or sets the current time.
  127. /// </summary>
  128. /// <value>The current time.</value>
  129. [Description("时间"), Category("自定义")]
  130. public DateTime CurrentTime
  131. {
  132. get { return currentTime; }
  133. set
  134. {
  135. currentTime = value;
  136. SetTimeToControl();
  137. }
  138. }
  139. /// <summary>
  140. /// Sets the time to control.
  141. /// </summary>
  142. private void SetTimeToControl()
  143. {
  144. this.txtYear.Text = currentTime.Year.ToString();
  145. this.txtMonth.Text = currentTime.Month.ToString().PadLeft(2, '0');
  146. this.txtDay.Text = currentTime.Day.ToString().PadLeft(2, '0');
  147. this.txtHour.Text = currentTime.Hour.ToString().PadLeft(2, '0');
  148. this.txtMinute.Text = currentTime.Minute.ToString().PadLeft(2, '0');
  149. }
  150. /// <summary>
  151. /// Initializes a new instance of the <see cref="UCDatePickerExt" /> class.
  152. /// </summary>
  153. public UCDatePickerExt()
  154. {
  155. InitializeComponent();
  156. }
  157. /// <summary>
  158. /// Handles the Load event of the UCDatePickerExt control.
  159. /// </summary>
  160. /// <param name="sender">The source of the event.</param>
  161. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  162. private void UCDatePickerExt_Load(object sender, EventArgs e)
  163. {
  164. SetTimeToControl();
  165. panel1.Height = this.txtDay.Height;
  166. panel1.Height = this.txtHour.Height;
  167. SetEvent(this);
  168. }
  169. /// <summary>
  170. /// Sets the event.
  171. /// </summary>
  172. /// <param name="c">The c.</param>
  173. private void SetEvent(Control c)
  174. {
  175. if (c != null)
  176. {
  177. c.MouseDown += c_MouseDown;
  178. foreach (Control item in c.Controls)
  179. {
  180. SetEvent(item);
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// Handles the MouseDown event of the c control.
  186. /// </summary>
  187. /// <param name="sender">The source of the event.</param>
  188. /// <param name="e">The <see cref="MouseEventArgs" /> instance containing the event data.</param>
  189. void c_MouseDown(object sender, MouseEventArgs e)
  190. {
  191. if (m_selectPan == null)
  192. {
  193. m_selectPan = new UCDateTimeSelectPan();
  194. m_selectPan.SelectedTimeEvent += uc_SelectedTimeEvent;
  195. m_selectPan.CancelTimeEvent += m_selectPan_CancelTimeEvent;
  196. }
  197. m_selectPan.CurrentTime = currentTime;
  198. m_selectPan.TimeType = m_type;
  199. m_frmAnchor = new Forms.FrmAnchor(this, m_selectPan);
  200. m_frmAnchor.Show(this.FindForm());
  201. }
  202. /// <summary>
  203. /// Handles the CancelTimeEvent event of the m_selectPan control.
  204. /// </summary>
  205. /// <param name="sender">The source of the event.</param>
  206. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  207. void m_selectPan_CancelTimeEvent(object sender, EventArgs e)
  208. {
  209. m_frmAnchor.Hide();
  210. }
  211. /// <summary>
  212. /// Handles the SelectedTimeEvent event of the uc control.
  213. /// </summary>
  214. /// <param name="sender">The source of the event.</param>
  215. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  216. void uc_SelectedTimeEvent(object sender, EventArgs e)
  217. {
  218. CurrentTime = m_selectPan.CurrentTime;
  219. m_frmAnchor.Hide();
  220. }
  221. /// <summary>
  222. /// Handles the TextChanged event of the txtYear control.
  223. /// </summary>
  224. /// <param name="sender">The source of the event.</param>
  225. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  226. private void txtYear_TextChanged(object sender, EventArgs e)
  227. {
  228. if (txtYear.Text.Length == 4)
  229. this.ActiveControl = txtMonth;
  230. }
  231. /// <summary>
  232. /// Handles the TextChanged event of the txtMonth control.
  233. /// </summary>
  234. /// <param name="sender">The source of the event.</param>
  235. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  236. private void txtMonth_TextChanged(object sender, EventArgs e)
  237. {
  238. if (txtMonth.Text.Length == 2 || txtMonth.Text.ToInt() >= 3)
  239. {
  240. this.ActiveControl = txtDay;
  241. }
  242. }
  243. /// <summary>
  244. /// Handles the TextChanged event of the txtDay control.
  245. /// </summary>
  246. /// <param name="sender">The source of the event.</param>
  247. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  248. private void txtDay_TextChanged(object sender, EventArgs e)
  249. {
  250. if (m_type == DateTimePickerType.Date)
  251. return;
  252. if (txtDay.Text.Length == 2 || txtDay.Text.ToInt() >= 4)
  253. {
  254. this.ActiveControl = txtHour;
  255. }
  256. }
  257. /// <summary>
  258. /// Handles the TextChanged event of the txtHour control.
  259. /// </summary>
  260. /// <param name="sender">The source of the event.</param>
  261. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  262. private void txtHour_TextChanged(object sender, EventArgs e)
  263. {
  264. if (txtHour.Text.Length == 2 || txtHour.Text.ToInt() >= 3)
  265. {
  266. this.ActiveControl = txtMinute;
  267. }
  268. }
  269. /// <summary>
  270. /// Handles the Leave event of the txtYear control.
  271. /// </summary>
  272. /// <param name="sender">The source of the event.</param>
  273. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  274. private void txtYear_Leave(object sender, EventArgs e)
  275. {
  276. if (txtYear.Text.ToInt() < 1990)
  277. {
  278. txtYear.Text = currentTime.Year.ToString();
  279. }
  280. currentTime = (txtYear.Text + currentTime.ToString("-MM-dd HH:mm:ss")).ToDate();
  281. }
  282. /// <summary>
  283. /// Handles the Leave event of the txtMonth control.
  284. /// </summary>
  285. /// <param name="sender">The source of the event.</param>
  286. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  287. private void txtMonth_Leave(object sender, EventArgs e)
  288. {
  289. if (txtMonth.Text.ToInt() < 1)
  290. {
  291. txtMonth.Text = currentTime.Month.ToString().PadLeft(2, '0');
  292. }
  293. txtMonth.Text = txtMonth.Text.PadLeft(2, '0');
  294. currentTime = (currentTime.ToString("yyyy-" + txtMonth.Text + "-dd HH:mm:ss")).ToDate();
  295. }
  296. /// <summary>
  297. /// Handles the Leave event of the txtDay control.
  298. /// </summary>
  299. /// <param name="sender">The source of the event.</param>
  300. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  301. private void txtDay_Leave(object sender, EventArgs e)
  302. {
  303. if (txtDay.Text.ToInt() < 1 || txtDay.Text.ToInt() > DateTime.DaysInMonth(currentTime.Year, currentTime.Month))
  304. {
  305. txtDay.Text = currentTime.Day.ToString().PadLeft(2, '0');
  306. }
  307. txtDay.Text = txtDay.Text.PadLeft(2, '0');
  308. currentTime = (currentTime.ToString("yyyy-MM-" + txtDay.Text + " HH:mm:ss")).ToDate();
  309. }
  310. /// <summary>
  311. /// Handles the Leave event of the txtHour control.
  312. /// </summary>
  313. /// <param name="sender">The source of the event.</param>
  314. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  315. private void txtHour_Leave(object sender, EventArgs e)
  316. {
  317. if (txtHour.Text.ToInt() < 1)
  318. {
  319. txtHour.Text = currentTime.Hour.ToString().PadLeft(2, '0');
  320. }
  321. txtHour.Text = txtHour.Text.PadLeft(2, '0');
  322. currentTime = (currentTime.ToString("yyyy-MM-dd " + txtHour.Text + ":mm:ss")).ToDate();
  323. }
  324. /// <summary>
  325. /// Handles the Leave event of the txtMinute control.
  326. /// </summary>
  327. /// <param name="sender">The source of the event.</param>
  328. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  329. private void txtMinute_Leave(object sender, EventArgs e)
  330. {
  331. if (txtMinute.Text.ToInt() < 1)
  332. {
  333. txtMinute.Text = currentTime.Minute.ToString().PadLeft(2, '0');
  334. }
  335. txtMinute.Text = txtMinute.Text.PadLeft(2, '0');
  336. currentTime = (currentTime.ToString("yyyy-MM-dd HH:" + txtMinute.Text + ":ss")).ToDate();
  337. }
  338. /// <summary>
  339. /// Handles the SizeChanged event of the txt control.
  340. /// </summary>
  341. /// <param name="sender">The source of the event.</param>
  342. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  343. private void txt_SizeChanged(object sender, EventArgs e)
  344. {
  345. panel1.Height = (sender as TextBoxEx).Height;
  346. }
  347. }
  348. }