📄 datetimeselectcontrol.cs
字号:
namespace Imps.Client.Pc.BizControls
{
using Imps.Client.Pc.Controls;
using Imps.Client.Resource;
using Imps.Client.Utils;
using Imps.Common;
using Imps.Utils;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class DateTimeSelectControl : UserControl
{
private DateTime? _datetime;
private DateTime _maxDate;
private DateTime _minDate;
private int _nullYear = 0x76c;
private XComboBox cboDay;
private XComboBox cboMonth;
private IContainer components;
private Label label1;
private Label label2;
private Label label3;
private XTextBox txtYear;
public event EventHandler SelectedDateTimeChange;
public event EventHandler YearTextChange;
public DateTimeSelectControl()
{
this.InitializeComponent();
this._minDate = DateTime.MinValue;
this._maxDate = DateTime.Today;
ControlHelper.SetAcceptDigitOnly(this.txtYear, true);
this.FillCombox(this.cboMonth, 12);
this.FillCombox(this.cboDay, 0x1f);
this.cboMonth.SelectedIndex = 0;
this.cboDay.SelectedIndex = 0;
}
private void cboDay_SelectedIndexChanged(object sender, EventArgs e)
{
if ((this.SelectedDay == 0) && (this.cboMonth.Items.Count > 0))
{
this.SelectedMonth = 0;
}
if (((this.SelectedMonth == 0) && (this.SelectedDay != 0)) && (this.cboMonth.Items.Count > 0))
{
this.SelectedMonth = 1;
}
this.InnerSetDateTime(this.GetSelectedDateTime(), false, true);
}
private void cboMonth_SelectedIndexChanged(object sender, EventArgs e)
{
if ((this.SelectedMonth == 0) && (this.cboDay.Items.Count > 0))
{
this.SelectedDay = 0;
}
if (((this.SelectedDay == 0) && (this.SelectedMonth != 0)) && (this.cboDay.Items.Count > 0))
{
this.SelectedDay = 1;
}
this.FillDaysOnYearMonthChanged();
this.InnerSetDateTime(this.GetSelectedDateTime(), false, true);
}
protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
}
private void FillCombox(ComboBox cbo, int max)
{
cbo.Items.Clear();
cbo.Items.Add(string.Empty);
for (int i = 1; i <= max; i++)
{
cbo.Items.Add(i);
}
}
private void FillDays(int maxDay)
{
int selectedIndex = this.cboDay.SelectedIndex;
int item = this.cboDay.Items.Count - 1;
if (item < maxDay)
{
while (item++ < maxDay)
{
this.cboDay.Items.Add(item);
}
}
else if (item > maxDay)
{
while (item-- > maxDay)
{
this.cboDay.Items.RemoveAt(item + 1);
}
if (selectedIndex >= maxDay)
{
this.cboDay.SelectedIndex = maxDay;
}
}
}
private void FillDaysOnYearMonthChanged()
{
int selectedMonth = this.SelectedMonth;
int maxDay = 0x1f;
switch (selectedMonth)
{
case 4:
case 6:
case 9:
case 11:
maxDay = 30;
break;
default:
if (selectedMonth == 2)
{
int year;
if (this.TryGetSelectedYear(out year))
{
maxDay = DateTime.IsLeapYear(year) ? 0x1d : 0x1c;
}
else
{
maxDay = 0x1d;
}
}
break;
}
this.FillDays(maxDay);
}
private DateTime? GetSelectedDateTime()
{
try
{
int year;
DateTime time;
if (!this.TryGetSelectedYear(out year))
{
if (this.txtYear.Text.Length > 0)
{
return null;
}
year = this.NullYear;
}
int selectedMonth = this.SelectedMonth;
int selectedDay = this.SelectedDay;
if (selectedMonth == 0)
{
selectedMonth = 1;
}
if (selectedDay == 0)
{
selectedDay = 1;
}
if ((!DateTime.TryParse(string.Format("{0}-{1}-{2}", year, selectedMonth, selectedDay), ref time) || (time < this.MinDateTime)) || (time > this.MaxDateTime))
{
return null;
}
return new DateTime?(time);
}
catch
{
return null;
}
}
private void InitializeComponent()
{
this.cboDay = new XComboBox();
this.txtYear = new XTextBox();
this.cboMonth = new XComboBox();
this.label1 = new Label();
this.label2 = new Label();
this.label3 = new Label();
base.SuspendLayout();
this.cboDay.BackColor = Color.White;
this.cboDay.DropDownStyle = ComboBoxStyle.DropDownList;
this.cboDay.EmptyTextTip = "";
this.cboDay.EmptyTextTipColor = Color.DarkGray;
this.cboDay.set_FormattingEnabled(true);
this.cboDay.Location = new Point(0xa7, 0);
this.cboDay.MaxDropDownItems = 20;
this.cboDay.Name = "cboDay";
this.cboDay.Size = new Size(0x2b, 20);
this.cboDay.TabIndex = 0x1c;
this.cboDay.SelectedIndexChanged += new EventHandler(this.cboDay_SelectedIndexChanged);
this.txtYear.BackColor = Color.White;
this.txtYear.BorderStyle = BorderStyle.FixedSingle;
this.txtYear.EmptyTextTip = "";
this.txtYear.EmptyTextTipColor = Color.DarkGray;
this.txtYear.Location = new Point(0, 0);
this.txtYear.MaxLength = 4;
this.txtYear.Name = "txtYear";
this.txtYear.Size = new Size(0x42, 0x15);
this.txtYear.TabIndex = 0x1a;
this.txtYear.TextChanged += new EventHandler(this.txtYear_TextChanged);
this.cboMonth.BackColor = Color.White;
this.cboMonth.DropDownStyle = ComboBoxStyle.DropDownList;
this.cboMonth.EmptyTextTip = "";
this.cboMonth.EmptyTextTipColor = Color.DarkGray;
this.cboMonth.set_FormattingEnabled(true);
this.cboMonth.Location = new Point(0x5f, 0);
this.cboMonth.MaxDropDownItems = 12;
this.cboMonth.Name = "cboMonth";
this.cboMonth.Size = new Size(0x2b, 20);
this.cboMonth.TabIndex = 0x1b;
this.cboMonth.SelectedIndexChanged += new EventHandler(this.cboMonth_SelectedIndexChanged);
this.label1.set_AutoSize(true);
this.label1.Location = new Point(0x90, 3);
this.label1.Name = "label1";
this.label1.Size = new Size(0x11, 12);
this.label1.TabIndex = 0x1d;
this.label1.Text = "月";
this.label2.set_AutoSize(true);
this.label2.Location = new Point(0x48, 3);
this.label2.Name = "label2";
this.label2.Size = new Size(0x11, 12);
this.label2.TabIndex = 30;
this.label2.Text = "年";
this.label3.set_AutoSize(true);
this.label3.Location = new Point(0xd8, 3);
this.label3.Name = "label3";
this.label3.Size = new Size(0x11, 12);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -