// *********************************************************************** // Assembly : HZH_Controls // Created : 2020-07-11 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** 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 HZH_Controls.Controls { public partial class UCDatePickerExt2 : UCControlBase { /// /// The m FRM anchor /// Forms.FrmAnchor m_frmAnchor; UCDateTimeSelectPan2 m_selectPan = null; /// /// The m type /// DateTimePickerType m_type = DateTimePickerType.DateTime; /// /// Gets or sets the type of the time. /// /// The type of the time. [Description("时间类型"), Category("自定义")] public DateTimePickerType TimeType { get { return m_type; } set { m_type = value; if (value == DateTimePickerType.DateTime) { txtYear.Visible = true; label1.Visible = true; txtMonth.Visible = true; label2.Visible = true; txtDay.Visible = true; label3.Visible = true; txtHour.Visible = true; label4.Visible = true; txtMinute.Visible = true; label5.Visible = true; } else if (value == DateTimePickerType.Date) { txtYear.Visible = true; label1.Visible = true; txtMonth.Visible = true; label2.Visible = true; txtDay.Visible = true; label3.Visible = true; txtHour.Visible = false; label4.Visible = false; txtMinute.Visible = false; label5.Visible = false; } else { txtYear.Visible = false; label1.Visible = false; txtMonth.Visible = false; label2.Visible = false; txtDay.Visible = false; label3.Visible = false; txtHour.Visible = true; label4.Visible = true; txtMinute.Visible = true; label5.Visible = true; } } } /// /// The current time /// private DateTime currentTime = DateTime.Now; /// /// The time font size /// private int timeFontSize = 20; /// /// Gets or sets the size of the time font. /// /// The size of the time font. [Description("时间字体大小"), Category("自定义")] public int TimeFontSize { get { return timeFontSize; } set { if (timeFontSize != value) { timeFontSize = value; foreach (Control c in panel1.Controls) { c.Font = new Font(c.Font.Name, value); } } } } /// /// Gets or sets the current time. /// /// The current time. [Description("时间"), Category("自定义")] [Localizable(true)] public DateTime CurrentTime { get { return currentTime; } set { currentTime = value; SetTimeToControl(); } } private Color selectColor = Color.FromArgb(255, 77, 59); /// /// 选中颜色 /// public Color SelectColor { get { return selectColor; } set { selectColor = value; } } public UCDatePickerExt2() { InitializeComponent(); } /// /// Sets the time to control. /// private void SetTimeToControl() { var y = CurrentTime.Year; var M = CurrentTime.Month; var d = CurrentTime.Day; var h = CurrentTime.Hour; var m = CurrentTime.Minute; this.txtYear.Text = y.ToString(); this.txtMonth.Text = M.ToString().PadLeft(2, '0'); this.txtDay.Text = d.ToString().PadLeft(2, '0'); this.txtHour.Text = h.ToString().PadLeft(2, '0'); this.txtMinute.Text = m.ToString().PadLeft(2, '0'); } private void UCDatePickerExt2_Load(object sender, EventArgs e) { SetTimeToControl(); panel1.Height = this.txtDay.Height; panel1.Width = this.Width - 6; SetEvent(this); } /// /// Sets the event. /// /// The c. private void SetEvent(Control c) { if (c != null) { c.MouseDown += c_MouseDown; foreach (Control item in c.Controls) { SetEvent(item); } } } /// /// Handles the MouseDown event of the c control. /// /// The source of the event. /// The instance containing the event data. void c_MouseDown(object sender, MouseEventArgs e) { if (m_selectPan == null) { m_selectPan = new UCDateTimeSelectPan2(); m_selectPan.SelectedTimeEvent += uc_SelectedTimeEvent; m_selectPan.CancelTimeEvent += m_selectPan_CancelTimeEvent; m_selectPan.SelectColor = selectColor; } m_selectPan.CurrentTime = currentTime; m_selectPan.TimeType = m_type; m_frmAnchor = new Forms.FrmAnchor(this, m_selectPan); m_frmAnchor.Show(this.FindForm()); } /// /// Handles the CancelTimeEvent event of the m_selectPan control. /// /// The source of the event. /// The instance containing the event data. void m_selectPan_CancelTimeEvent(object sender, EventArgs e) { m_frmAnchor.Hide(); } /// /// Handles the SelectedTimeEvent event of the uc control. /// /// The source of the event. /// The instance containing the event data. void uc_SelectedTimeEvent(object sender, EventArgs e) { CurrentTime = m_selectPan.CurrentTime; m_frmAnchor.Hide(); } /// /// Handles the TextChanged event of the txtYear control. /// /// The source of the event. /// The instance containing the event data. private void txtYear_TextChanged(object sender, EventArgs e) { if (txtYear.Text.Length == 4) this.ActiveControl = txtMonth; } /// /// Handles the TextChanged event of the txtMonth control. /// /// The source of the event. /// The instance containing the event data. private void txtMonth_TextChanged(object sender, EventArgs e) { if (txtMonth.Text.Length == 2 || txtMonth.Text.ToInt() >= 3) { this.ActiveControl = txtDay; } } /// /// Handles the TextChanged event of the txtDay control. /// /// The source of the event. /// The instance containing the event data. private void txtDay_TextChanged(object sender, EventArgs e) { if (m_type == DateTimePickerType.Date) return; if (txtDay.Text.Length == 2 || txtDay.Text.ToInt() >= 4) { this.ActiveControl = txtHour; } } /// /// Handles the TextChanged event of the txtHour control. /// /// The source of the event. /// The instance containing the event data. private void txtHour_TextChanged(object sender, EventArgs e) { if (txtHour.Text.Length == 2 || txtHour.Text.ToInt() >= 3) { this.ActiveControl = txtMinute; } } /// /// Handles the Leave event of the txtYear control. /// /// The source of the event. /// The instance containing the event data. private void txtYear_Leave(object sender, EventArgs e) { if (txtYear.Text.ToInt() < 1990) { txtYear.Text = currentTime.Year.ToString(); } currentTime = (txtYear.Text + currentTime.ToString("-MM-dd HH:mm:ss")).ToDate(); } /// /// Handles the Leave event of the txtMonth control. /// /// The source of the event. /// The instance containing the event data. private void txtMonth_Leave(object sender, EventArgs e) { if (txtMonth.Text.ToInt() < 1) { txtMonth.Text = currentTime.Month.ToString().PadLeft(2, '0'); } txtMonth.Text = txtMonth.Text.PadLeft(2, '0'); currentTime = (currentTime.ToString("yyyy-" + txtMonth.Text + "-dd HH:mm:ss")).ToDate(); } /// /// Handles the Leave event of the txtDay control. /// /// The source of the event. /// The instance containing the event data. private void txtDay_Leave(object sender, EventArgs e) { if (txtDay.Text.ToInt() < 1 || txtDay.Text.ToInt() > DateTime.DaysInMonth(currentTime.Year, currentTime.Month)) { txtDay.Text = currentTime.Day.ToString().PadLeft(2, '0'); } txtDay.Text = txtDay.Text.PadLeft(2, '0'); currentTime = (currentTime.ToString("yyyy-MM-" + txtDay.Text + " HH:mm:ss")).ToDate(); } /// /// Handles the Leave event of the txtHour control. /// /// The source of the event. /// The instance containing the event data. private void txtHour_Leave(object sender, EventArgs e) { if (txtHour.Text.ToInt() < 1) { txtHour.Text = currentTime.Hour.ToString().PadLeft(2, '0'); } txtHour.Text = txtHour.Text.PadLeft(2, '0'); currentTime = (currentTime.ToString("yyyy-MM-dd " + txtHour.Text + ":mm:ss")).ToDate(); } /// /// Handles the Leave event of the txtMinute control. /// /// The source of the event. /// The instance containing the event data. private void txtMinute_Leave(object sender, EventArgs e) { } /// /// Handles the SizeChanged event of the txt control. /// /// The source of the event. /// The instance containing the event data. private void txt_SizeChanged(object sender, EventArgs e) { panel1.Height = (sender as TextBoxEx).Height; } } }