123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace LYFZ.ComponentLibrary
- {
- public partial class DateYear : UserControl
- {
- LYFZ.ComponentLibrary.ButtonDownArrow btnDown = new ButtonDownArrow();
- public DateYear()
- {
- InitializeComponent();
- this.Load += DateYearMonth_Load;
- this.Leave += DateYearMonth_Leave;
- this.tblPanelYear.Leave += tblPanelYear_Leave;
- this.lblUp.Click += lblUp_Click;
- this.lblNext.Click += lblNext_Click;
- this.txtYearValue.KeyDown += txtYearValue_KeyDown;
- this.lblToYear.Click += panelToyear_Click;
- this.panellblToYear.Click += panelToyear_Click;
- this.panellblToYear.MouseEnter += lblToYear_MouseEnter;
- this.panellblToYear.MouseLeave += lblToYear_MouseLeave;
- this.lblToYear.MouseEnter += lblToYear_MouseEnter;
- this.lblToYear.MouseLeave += lblToYear_MouseLeave;
- this.lblYear.MouseEnter += lblYear_MouseEnter;
- this.lblYear.MouseLeave += lblYear_MouseLeave;
- btnDown.Size = new Size(18, this.txtYearValue.Height - 2);
- btnDown.Dock = DockStyle.Right;
- btnDown.IsDown = true;
- btnDown.Click += btnDown_Click;
- this.txtYearValue.Controls.Add(btnDown);
- this.SetHeight();
- }
- private string _strdatevalue;
- /// <summary>
- /// 设置或获取值
- /// </summary>
- public string StrDateValue
- {
- get
- {
- if (this.txtYearValue.Text.Trim() != "-")
- { return this.txtYearValue.Text.Trim(); }
- else
- { return ""; }
- }
- set
- {
- _strdatevalue = value;
- if (_strdatevalue != "")
- {
- this.DetectedDate(_strdatevalue);
- }
- }
- }
- /// <summary>
- /// 当前选择的年份
- /// </summary>
- int CurrentYear = 0;
- /// <summary>
- /// 窗体加载事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DateYearMonth_Load(object sender, EventArgs e)
- {
- if (this.CurrentYear == 0)
- { this.CurrentYear = SDateTime.Now.Year; }
- this.PaintingCharts_Year();
- this.panellblToYear.BorderStyle = BorderStyle.FixedSingle;
- this.panellblToYear.BorderColor = this.GetColor(ColorType.当前年移过字体);
- this.lblToYear.Text = "当前年:" + SDateTime.Now.Year;
- this.ParentForm.Click += ParentForm_Click;
- foreach (Control cont in this.ParentForm.Controls)
- {
- if (!(cont is DateYearMonth))
- { cont.Click += DateYearMonth_Click; }
- }
- }
- /// <summary>
- /// 父窗体的点击事件隐藏控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void ParentForm_Click(object sender, EventArgs e)
- { this.DateYearMonth_Click(this, null); }
- /// <summary>
- /// 父窗体的控件的点击事件隐藏控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DateYearMonth_Click(object sender, EventArgs e)
- { this.SetHeight(); }
- /// <summary>
- /// 上一年
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblUp_Click(object sender, EventArgs e)
- {
- this.CurrentYear = Convert.ToInt32(this.lblYear.Text.Trim().Split('-')[0]) - 12;
- this.CleartblPanelYearControls();
- }
- /// <summary>
- /// 下一年
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblNext_Click(object sender, EventArgs e)
- {
- this.CurrentYear = Convert.ToInt32(this.lblYear.Text.Trim().Split('-')[0]) + 12;
- this.CleartblPanelYearControls();
- }
- /// <summary>
- /// 清空年份选择器
- /// </summary>
- void CleartblPanelYearControls()
- {
- this.tblPanelYear.Controls.Clear();
- this.PaintingCharts_Year();
- }
- /// <summary>
- /// 数据处理_年
- /// </summary>
- private void PaintingCharts_Year()
- {
- Panel panel = null;
- Label lable = null;
- this.lblYear.Text = this.CurrentYear + "-" + (this.CurrentYear + 11);
- this.lblYear.Location = new Point(73, this.lblYear.Location.Y);
- int ForInt = 0; ;
- for (int i = 0; i < this.tblPanelYear.RowCount; i++)
- {
- for (int j = 0; j < this.tblPanelYear.ColumnCount; j++)
- {
- #region 每日板块
- panel = new Panel();
- if (this.CurrentYear == this.CurrentYear + ForInt)
- { panel.BackColor = this.GetColor(ColorType.选中); }
- else
- { panel.BackColor = this.GetColor(ColorType.默认); }
- panel.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
- panel.Name = "panel_" + this.CurrentYear + ForInt;
- panel.Tag = this.CurrentYear + ForInt;
- panel.Dock = DockStyle.Fill;
- panel.MouseEnter += panel_MouseEnter;
- panel.MouseLeave += panel_MouseLeave;
- panel.Click += panel_Click;
- //每年
- lable = new Label();
- lable.Name = "lblYear" + this.CurrentYear + ForInt;
- lable.Font = new System.Drawing.Font("微软雅黑", 9);
- lable.ForeColor = this.GetColor(ColorType.字体);
- lable.AutoSize = false;
- lable.Size = new System.Drawing.Size(50, 20);
- lable.Text = (this.CurrentYear + ForInt).ToString();
- lable.Tag = this.CurrentYear + ForInt;
- lable.Location = new Point(8, 12);
- lable.MouseEnter += lable_MouseEnter;
- lable.MouseLeave += lable_MouseLeave;
- lable.Click += lable_Click;
- panel.Controls.Add(lable);
- #endregion
- this.tblPanelYear.Controls.Add(panel);
- ForInt++;
- }
- }
- this.panelYear.BringToFront();
- }
- /// <summary>
- /// lbl光标离开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lable_MouseLeave(object sender, EventArgs e)
- {
- Label lbl = sender as Label;
- if (Convert.ToInt32(lbl.Tag) == this.CurrentYear)
- { lbl.Parent.BackColor = this.GetColor(ColorType.选中); }
- else
- { lbl.Parent.BackColor = this.GetColor(ColorType.默认); }
- }
- /// <summary>
- /// lbl光标进入事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lable_MouseEnter(object sender, EventArgs e)
- {
- Label lbl = sender as Label;
- lbl.Parent.BackColor = this.GetColor(ColorType.移过);
- }
- /// <summary>
- /// 层光标离开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void panel_MouseLeave(object sender, EventArgs e)
- {
- Panel panel = sender as Panel;
- if (Convert.ToInt32(panel.Tag) == this.CurrentYear)
- { panel.BackColor = this.GetColor(ColorType.选中); }
- else
- { panel.BackColor = this.GetColor(ColorType.默认); }
- }
- /// <summary>
- /// 层光标进入事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void panel_MouseEnter(object sender, EventArgs e)
- {
- Panel panel = sender as Panel;
- panel.BackColor = this.GetColor(ColorType.移过);
- }
- /// <summary>
- /// 当前年光标进入事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblToYear_MouseEnter(object sender, EventArgs e)
- {
- this.lblToYear.ForeColor = this.GetColor(ColorType.当前年移过字体);
- }
- /// <summary>
- /// 当前年光标离开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblToYear_MouseLeave(object sender, EventArgs e)
- {
- this.lblToYear.ForeColor = this.GetColor(ColorType.当前年离开字体);
- }
- /// <summary>
- /// 当前提示选择的年月提示光标进入事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblYear_MouseEnter(object sender, EventArgs e)
- {
- this.lblYear.ForeColor = this.GetColor(ColorType.当前年移过字体);
- }
- /// <summary>
- /// 当前提示选择的年月提示光标离开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lblYear_MouseLeave(object sender, EventArgs e)
- {
- this.lblYear.ForeColor = this.GetColor(ColorType.当前年离开字体);
- }
- /// <summary>
- /// 选择年
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void lable_Click(object sender, EventArgs e)
- { this.SelectCurrentYear(sender, false); }
- /// <summary>
- /// 选择年
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void panel_Click(object sender, EventArgs e)
- { this.SelectCurrentYear(sender, true); }
- /// <summary>
- /// 选择当前年
- /// </summary>
- /// <param name="objControls"></param>
- /// <param name="IsPanel"></param>
- void SelectCurrentYear(object objControls, bool IsPanel)
- {
- if (IsPanel)
- {
- Panel panel = objControls as Panel;
- if (panel != null)
- {
- this.CurrentYear = Convert.ToInt32(panel.Tag);
- this.txtYearValue.Text = this.CurrentYear.ToString();
- }
- }
- else
- {
- Label lbl = objControls as Label;
- if (lbl != null)
- {
- this.CurrentYear = Convert.ToInt32(lbl.Tag);
- this.txtYearValue.Text = this.CurrentYear.ToString();
- }
- }
- this.btnDown.IsDown = true;
- this.SetHeight();
- }
- /// <summary>
- /// 选择今年
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void panelToyear_Click(object sender, EventArgs e)
- {
- this.CurrentYear = SDateTime.Now.Year;
- this.txtYearValue.Text = this.CurrentYear.ToString();
- this.btnDown.IsDown = true;
- this.SetHeight();
- }
- /// <summary>
- /// 点击展开
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void btnDown_Click(object sender, EventArgs e)
- {
- this.btnDown.Parent.Parent.Anchor = AnchorStyles.Left | AnchorStyles.Top;
- if (this.Height > 25)
- {
- this.btnDown.IsDown = true;
- this.SetHeight();
- this.txtYearValue.Focus();
- }
- else
- {
- this.btnDown.IsDown = false;
- if (this.panelYearValue.Width < 216)
- { this.Size = new Size(216, 202); }
- else
- { this.Size = new Size(this.panelYearValue.Width, 202); }
- int YearLog = this.CurrentYear;
- if (this.txtYearValue.Text.Trim() != "")
- {
- this.DetectedDate(this.txtYearValue.Text.Trim());
- for (int i = 0; i < 12; i++)
- {
- Panel newpanel = (Panel)tblPanelYear.Controls[i];
- if (newpanel != null)
- {
- if (Convert.ToInt32(newpanel.Tag) == this.CurrentYear)
- { newpanel.BackColor = this.GetColor(ColorType.选中); }
- else
- { newpanel.BackColor = this.GetColor(ColorType.默认); }
- }
- }
- }
- else
- { this.CurrentYear = SDateTime.Now.Year; }
- if (YearLog != this.CurrentYear)
- {
- this.tblPanelYear.Controls.Clear();
- this.PaintingCharts_Year();
- }
- this.tblPanelYear.BringToFront();
- this.BringToFront();
- this.Focus();
- }
- }
- /// <summary>
- /// 输入时隐藏控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void txtYearValue_KeyDown(object sender, KeyEventArgs e)
- { this.SetHeight(); }
- /// <summary>
- /// 离开事件隐藏控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void tblPanelYear_Leave(object sender, EventArgs e)
- { this.SetHeight(); }
- /// <summary>
- /// 离开事件隐藏控件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- void DateYearMonth_Leave(object sender, EventArgs e)
- { this.SetHeight(); }
- /// <summary>
- /// 设置当前高
- /// </summary>
- void SetHeight()
- { this.Size = new Size(this.panelYearValue.Width, 25); }
- /// <summary>
- /// 在值的前面用O补充
- /// </summary>
- /// <param name="Value">要操作的值</param>
- /// <param name="ValueLength">值要达到的长度</param>
- /// <returns></returns>
- string GetBeforeAddZero(int Value, int ValueLength)
- {
- string StrValue = Value.ToString().Trim();
- while (StrValue.Length < ValueLength)
- { StrValue = "0" + StrValue; }
- return StrValue;
- }
- /// <summary>
- /// 检测日期
- /// </summary>
- void DetectedDate(string DateValue)
- {
- if (DateValue.Trim().Length == 4)
- {
- int intYear = Convert.ToInt32(DateValue);
- if (intYear < 1753)
- {
- this.CurrentYear = 1753;
- this.txtYearValue.Text = this.CurrentYear.ToString();
- }
- else
- { this.CurrentYear = intYear; }
- }
- else
- { this.CurrentYear = SDateTime.Now.Year; }
- }
- /// <summary>
- /// 获取颜色
- /// </summary>
- /// <param name="CurrentColorType"></param>
- /// <returns></returns>
- Color GetColor(ColorType CurrentColorType)
- {
- switch (CurrentColorType)
- {
- case ColorType.选中:
- return Color.LightPink;
- case ColorType.移过:
- return Color.SkyBlue;
- case ColorType.字体:
- return Color.White;
- case ColorType.当前年移过字体:
- return Color.Blue;
- case ColorType.当前年离开字体:
- return Color.Black;
- default:
- return Color.Gainsboro;
- }
- }
- /// <summary>
- /// 颜色枚举
- /// </summary>
- enum ColorType
- {
- 默认,
- 选中,
- 移过,
- 字体,
- 当前年移过字体,
- 当前年离开字体,
- }
- }
- }
|