DateYear.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class DateYear : UserControl
  12. {
  13. LYFZ.ComponentLibrary.ButtonDownArrow btnDown = new ButtonDownArrow();
  14. public DateYear()
  15. {
  16. InitializeComponent();
  17. this.Load += DateYearMonth_Load;
  18. this.Leave += DateYearMonth_Leave;
  19. this.tblPanelYear.Leave += tblPanelYear_Leave;
  20. this.lblUp.Click += lblUp_Click;
  21. this.lblNext.Click += lblNext_Click;
  22. this.txtYearValue.KeyDown += txtYearValue_KeyDown;
  23. this.lblToYear.Click += panelToyear_Click;
  24. this.panellblToYear.Click += panelToyear_Click;
  25. this.panellblToYear.MouseEnter += lblToYear_MouseEnter;
  26. this.panellblToYear.MouseLeave += lblToYear_MouseLeave;
  27. this.lblToYear.MouseEnter += lblToYear_MouseEnter;
  28. this.lblToYear.MouseLeave += lblToYear_MouseLeave;
  29. this.lblYear.MouseEnter += lblYear_MouseEnter;
  30. this.lblYear.MouseLeave += lblYear_MouseLeave;
  31. btnDown.Size = new Size(18, this.txtYearValue.Height - 2);
  32. btnDown.Dock = DockStyle.Right;
  33. btnDown.IsDown = true;
  34. btnDown.Click += btnDown_Click;
  35. this.txtYearValue.Controls.Add(btnDown);
  36. this.SetHeight();
  37. }
  38. private string _strdatevalue;
  39. /// <summary>
  40. /// 设置或获取值
  41. /// </summary>
  42. public string StrDateValue
  43. {
  44. get
  45. {
  46. if (this.txtYearValue.Text.Trim() != "-")
  47. { return this.txtYearValue.Text.Trim(); }
  48. else
  49. { return ""; }
  50. }
  51. set
  52. {
  53. _strdatevalue = value;
  54. if (_strdatevalue != "")
  55. {
  56. this.DetectedDate(_strdatevalue);
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// 当前选择的年份
  62. /// </summary>
  63. int CurrentYear = 0;
  64. /// <summary>
  65. /// 窗体加载事件
  66. /// </summary>
  67. /// <param name="sender"></param>
  68. /// <param name="e"></param>
  69. void DateYearMonth_Load(object sender, EventArgs e)
  70. {
  71. if (this.CurrentYear == 0)
  72. { this.CurrentYear = SDateTime.Now.Year; }
  73. this.PaintingCharts_Year();
  74. this.panellblToYear.BorderStyle = BorderStyle.FixedSingle;
  75. this.panellblToYear.BorderColor = this.GetColor(ColorType.当前年移过字体);
  76. this.lblToYear.Text = "当前年:" + SDateTime.Now.Year;
  77. this.ParentForm.Click += ParentForm_Click;
  78. foreach (Control cont in this.ParentForm.Controls)
  79. {
  80. if (!(cont is DateYearMonth))
  81. { cont.Click += DateYearMonth_Click; }
  82. }
  83. }
  84. /// <summary>
  85. /// 父窗体的点击事件隐藏控件
  86. /// </summary>
  87. /// <param name="sender"></param>
  88. /// <param name="e"></param>
  89. void ParentForm_Click(object sender, EventArgs e)
  90. { this.DateYearMonth_Click(this, null); }
  91. /// <summary>
  92. /// 父窗体的控件的点击事件隐藏控件
  93. /// </summary>
  94. /// <param name="sender"></param>
  95. /// <param name="e"></param>
  96. void DateYearMonth_Click(object sender, EventArgs e)
  97. { this.SetHeight(); }
  98. /// <summary>
  99. /// 上一年
  100. /// </summary>
  101. /// <param name="sender"></param>
  102. /// <param name="e"></param>
  103. void lblUp_Click(object sender, EventArgs e)
  104. {
  105. this.CurrentYear = Convert.ToInt32(this.lblYear.Text.Trim().Split('-')[0]) - 12;
  106. this.CleartblPanelYearControls();
  107. }
  108. /// <summary>
  109. /// 下一年
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. void lblNext_Click(object sender, EventArgs e)
  114. {
  115. this.CurrentYear = Convert.ToInt32(this.lblYear.Text.Trim().Split('-')[0]) + 12;
  116. this.CleartblPanelYearControls();
  117. }
  118. /// <summary>
  119. /// 清空年份选择器
  120. /// </summary>
  121. void CleartblPanelYearControls()
  122. {
  123. this.tblPanelYear.Controls.Clear();
  124. this.PaintingCharts_Year();
  125. }
  126. /// <summary>
  127. /// 数据处理_年
  128. /// </summary>
  129. private void PaintingCharts_Year()
  130. {
  131. Panel panel = null;
  132. Label lable = null;
  133. this.lblYear.Text = this.CurrentYear + "-" + (this.CurrentYear + 11);
  134. this.lblYear.Location = new Point(73, this.lblYear.Location.Y);
  135. int ForInt = 0; ;
  136. for (int i = 0; i < this.tblPanelYear.RowCount; i++)
  137. {
  138. for (int j = 0; j < this.tblPanelYear.ColumnCount; j++)
  139. {
  140. #region 每日板块
  141. panel = new Panel();
  142. if (this.CurrentYear == this.CurrentYear + ForInt)
  143. { panel.BackColor = this.GetColor(ColorType.选中); }
  144. else
  145. { panel.BackColor = this.GetColor(ColorType.默认); }
  146. panel.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
  147. panel.Name = "panel_" + this.CurrentYear + ForInt;
  148. panel.Tag = this.CurrentYear + ForInt;
  149. panel.Dock = DockStyle.Fill;
  150. panel.MouseEnter += panel_MouseEnter;
  151. panel.MouseLeave += panel_MouseLeave;
  152. panel.Click += panel_Click;
  153. //每年
  154. lable = new Label();
  155. lable.Name = "lblYear" + this.CurrentYear + ForInt;
  156. lable.Font = new System.Drawing.Font("微软雅黑", 9);
  157. lable.ForeColor = this.GetColor(ColorType.字体);
  158. lable.AutoSize = false;
  159. lable.Size = new System.Drawing.Size(50, 20);
  160. lable.Text = (this.CurrentYear + ForInt).ToString();
  161. lable.Tag = this.CurrentYear + ForInt;
  162. lable.Location = new Point(8, 12);
  163. lable.MouseEnter += lable_MouseEnter;
  164. lable.MouseLeave += lable_MouseLeave;
  165. lable.Click += lable_Click;
  166. panel.Controls.Add(lable);
  167. #endregion
  168. this.tblPanelYear.Controls.Add(panel);
  169. ForInt++;
  170. }
  171. }
  172. this.panelYear.BringToFront();
  173. }
  174. /// <summary>
  175. /// lbl光标离开事件
  176. /// </summary>
  177. /// <param name="sender"></param>
  178. /// <param name="e"></param>
  179. void lable_MouseLeave(object sender, EventArgs e)
  180. {
  181. Label lbl = sender as Label;
  182. if (Convert.ToInt32(lbl.Tag) == this.CurrentYear)
  183. { lbl.Parent.BackColor = this.GetColor(ColorType.选中); }
  184. else
  185. { lbl.Parent.BackColor = this.GetColor(ColorType.默认); }
  186. }
  187. /// <summary>
  188. /// lbl光标进入事件
  189. /// </summary>
  190. /// <param name="sender"></param>
  191. /// <param name="e"></param>
  192. void lable_MouseEnter(object sender, EventArgs e)
  193. {
  194. Label lbl = sender as Label;
  195. lbl.Parent.BackColor = this.GetColor(ColorType.移过);
  196. }
  197. /// <summary>
  198. /// 层光标离开事件
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. void panel_MouseLeave(object sender, EventArgs e)
  203. {
  204. Panel panel = sender as Panel;
  205. if (Convert.ToInt32(panel.Tag) == this.CurrentYear)
  206. { panel.BackColor = this.GetColor(ColorType.选中); }
  207. else
  208. { panel.BackColor = this.GetColor(ColorType.默认); }
  209. }
  210. /// <summary>
  211. /// 层光标进入事件
  212. /// </summary>
  213. /// <param name="sender"></param>
  214. /// <param name="e"></param>
  215. void panel_MouseEnter(object sender, EventArgs e)
  216. {
  217. Panel panel = sender as Panel;
  218. panel.BackColor = this.GetColor(ColorType.移过);
  219. }
  220. /// <summary>
  221. /// 当前年光标进入事件
  222. /// </summary>
  223. /// <param name="sender"></param>
  224. /// <param name="e"></param>
  225. void lblToYear_MouseEnter(object sender, EventArgs e)
  226. {
  227. this.lblToYear.ForeColor = this.GetColor(ColorType.当前年移过字体);
  228. }
  229. /// <summary>
  230. /// 当前年光标离开事件
  231. /// </summary>
  232. /// <param name="sender"></param>
  233. /// <param name="e"></param>
  234. void lblToYear_MouseLeave(object sender, EventArgs e)
  235. {
  236. this.lblToYear.ForeColor = this.GetColor(ColorType.当前年离开字体);
  237. }
  238. /// <summary>
  239. /// 当前提示选择的年月提示光标进入事件
  240. /// </summary>
  241. /// <param name="sender"></param>
  242. /// <param name="e"></param>
  243. void lblYear_MouseEnter(object sender, EventArgs e)
  244. {
  245. this.lblYear.ForeColor = this.GetColor(ColorType.当前年移过字体);
  246. }
  247. /// <summary>
  248. /// 当前提示选择的年月提示光标离开事件
  249. /// </summary>
  250. /// <param name="sender"></param>
  251. /// <param name="e"></param>
  252. void lblYear_MouseLeave(object sender, EventArgs e)
  253. {
  254. this.lblYear.ForeColor = this.GetColor(ColorType.当前年离开字体);
  255. }
  256. /// <summary>
  257. /// 选择年
  258. /// </summary>
  259. /// <param name="sender"></param>
  260. /// <param name="e"></param>
  261. void lable_Click(object sender, EventArgs e)
  262. { this.SelectCurrentYear(sender, false); }
  263. /// <summary>
  264. /// 选择年
  265. /// </summary>
  266. /// <param name="sender"></param>
  267. /// <param name="e"></param>
  268. private void panel_Click(object sender, EventArgs e)
  269. { this.SelectCurrentYear(sender, true); }
  270. /// <summary>
  271. /// 选择当前年
  272. /// </summary>
  273. /// <param name="objControls"></param>
  274. /// <param name="IsPanel"></param>
  275. void SelectCurrentYear(object objControls, bool IsPanel)
  276. {
  277. if (IsPanel)
  278. {
  279. Panel panel = objControls as Panel;
  280. if (panel != null)
  281. {
  282. this.CurrentYear = Convert.ToInt32(panel.Tag);
  283. this.txtYearValue.Text = this.CurrentYear.ToString();
  284. }
  285. }
  286. else
  287. {
  288. Label lbl = objControls as Label;
  289. if (lbl != null)
  290. {
  291. this.CurrentYear = Convert.ToInt32(lbl.Tag);
  292. this.txtYearValue.Text = this.CurrentYear.ToString();
  293. }
  294. }
  295. this.btnDown.IsDown = true;
  296. this.SetHeight();
  297. }
  298. /// <summary>
  299. /// 选择今年
  300. /// </summary>
  301. /// <param name="sender"></param>
  302. /// <param name="e"></param>
  303. void panelToyear_Click(object sender, EventArgs e)
  304. {
  305. this.CurrentYear = SDateTime.Now.Year;
  306. this.txtYearValue.Text = this.CurrentYear.ToString();
  307. this.btnDown.IsDown = true;
  308. this.SetHeight();
  309. }
  310. /// <summary>
  311. /// 点击展开
  312. /// </summary>
  313. /// <param name="sender"></param>
  314. /// <param name="e"></param>
  315. void btnDown_Click(object sender, EventArgs e)
  316. {
  317. this.btnDown.Parent.Parent.Anchor = AnchorStyles.Left | AnchorStyles.Top;
  318. if (this.Height > 25)
  319. {
  320. this.btnDown.IsDown = true;
  321. this.SetHeight();
  322. this.txtYearValue.Focus();
  323. }
  324. else
  325. {
  326. this.btnDown.IsDown = false;
  327. if (this.panelYearValue.Width < 216)
  328. { this.Size = new Size(216, 202); }
  329. else
  330. { this.Size = new Size(this.panelYearValue.Width, 202); }
  331. int YearLog = this.CurrentYear;
  332. if (this.txtYearValue.Text.Trim() != "")
  333. {
  334. this.DetectedDate(this.txtYearValue.Text.Trim());
  335. for (int i = 0; i < 12; i++)
  336. {
  337. Panel newpanel = (Panel)tblPanelYear.Controls[i];
  338. if (newpanel != null)
  339. {
  340. if (Convert.ToInt32(newpanel.Tag) == this.CurrentYear)
  341. { newpanel.BackColor = this.GetColor(ColorType.选中); }
  342. else
  343. { newpanel.BackColor = this.GetColor(ColorType.默认); }
  344. }
  345. }
  346. }
  347. else
  348. { this.CurrentYear = SDateTime.Now.Year; }
  349. if (YearLog != this.CurrentYear)
  350. {
  351. this.tblPanelYear.Controls.Clear();
  352. this.PaintingCharts_Year();
  353. }
  354. this.tblPanelYear.BringToFront();
  355. this.BringToFront();
  356. this.Focus();
  357. }
  358. }
  359. /// <summary>
  360. /// 输入时隐藏控件
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. void txtYearValue_KeyDown(object sender, KeyEventArgs e)
  365. { this.SetHeight(); }
  366. /// <summary>
  367. /// 离开事件隐藏控件
  368. /// </summary>
  369. /// <param name="sender"></param>
  370. /// <param name="e"></param>
  371. void tblPanelYear_Leave(object sender, EventArgs e)
  372. { this.SetHeight(); }
  373. /// <summary>
  374. /// 离开事件隐藏控件
  375. /// </summary>
  376. /// <param name="sender"></param>
  377. /// <param name="e"></param>
  378. void DateYearMonth_Leave(object sender, EventArgs e)
  379. { this.SetHeight(); }
  380. /// <summary>
  381. /// 设置当前高
  382. /// </summary>
  383. void SetHeight()
  384. { this.Size = new Size(this.panelYearValue.Width, 25); }
  385. /// <summary>
  386. /// 在值的前面用O补充
  387. /// </summary>
  388. /// <param name="Value">要操作的值</param>
  389. /// <param name="ValueLength">值要达到的长度</param>
  390. /// <returns></returns>
  391. string GetBeforeAddZero(int Value, int ValueLength)
  392. {
  393. string StrValue = Value.ToString().Trim();
  394. while (StrValue.Length < ValueLength)
  395. { StrValue = "0" + StrValue; }
  396. return StrValue;
  397. }
  398. /// <summary>
  399. /// 检测日期
  400. /// </summary>
  401. void DetectedDate(string DateValue)
  402. {
  403. if (DateValue.Trim().Length == 4)
  404. {
  405. int intYear = Convert.ToInt32(DateValue);
  406. if (intYear < 1753)
  407. {
  408. this.CurrentYear = 1753;
  409. this.txtYearValue.Text = this.CurrentYear.ToString();
  410. }
  411. else
  412. { this.CurrentYear = intYear; }
  413. }
  414. else
  415. { this.CurrentYear = SDateTime.Now.Year; }
  416. }
  417. /// <summary>
  418. /// 获取颜色
  419. /// </summary>
  420. /// <param name="CurrentColorType"></param>
  421. /// <returns></returns>
  422. Color GetColor(ColorType CurrentColorType)
  423. {
  424. switch (CurrentColorType)
  425. {
  426. case ColorType.选中:
  427. return Color.LightPink;
  428. case ColorType.移过:
  429. return Color.SkyBlue;
  430. case ColorType.字体:
  431. return Color.White;
  432. case ColorType.当前年移过字体:
  433. return Color.Blue;
  434. case ColorType.当前年离开字体:
  435. return Color.Black;
  436. default:
  437. return Color.Gainsboro;
  438. }
  439. }
  440. /// <summary>
  441. /// 颜色枚举
  442. /// </summary>
  443. enum ColorType
  444. {
  445. 默认,
  446. 选中,
  447. 移过,
  448. 字体,
  449. 当前年移过字体,
  450. 当前年离开字体,
  451. }
  452. }
  453. }