using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace UAS_MES_NEW.CustomControl.TimePickerWithCombo { public partial class TimePickerWithCombo : UserControl { public TimePickerWithCombo() { InitializeComponent(); setTimePickerNull(); } private string beginTime; private string endTime; private string beginTimeText; private string endTimeText; public string BeginTime { get { return beginTime; } set { beginTime = value; } } public string EndTime { get { return endTime; } set { endTime = value; } } public string BeginTimeText { get { return beginTimeText; } set { beginTimeText = value; } } public string EndTimeText { get { return endTimeText; } set { endTimeText = value; } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string time = comboBox1.SelectedItem.ToString(); string beginTime = ""; string endTime = ""; switch (time) { case "今天": dateTimePicker2.Value = DateTime.Now.Date; dateTimePicker3.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1); beginTime = dateTimePicker2.Value.ToString(); endTime = dateTimePicker3.Value.ToString(); break; case "昨天": dateTimePicker2.Value = DateTime.Now.AddDays(-1).Date; dateTimePicker3.Value = DateTime.Now.Date.AddSeconds(-1); beginTime = dateTimePicker2.Value.ToString(); endTime = dateTimePicker3.Value.ToString(); break; case "本月": string dateMonthNow = DateTime.Now.Year + " " + DateTime.Now.Month + " 01"; dateTimePicker2.Text = dateMonthNow; dateTimePicker3.Value = dateTimePicker2.Value.AddMonths(1).AddSeconds(-1); break; case "上个月": string dateMonthLast = DateTime.Now.Year + " " + DateTime.Now.AddMonths(-1).Month + " 01"; dateTimePicker2.Text = dateMonthLast; dateTimePicker3.Value = dateTimePicker2.Value.AddMonths(1).AddSeconds(-1); break; case "本年度": string dateYearNow = DateTime.Now.Year + " /01/01"; dateTimePicker2.Text = dateYearNow; dateTimePicker3.Value = dateTimePicker2.Value.AddYears(1).AddSeconds(-1); break; case "上年度": string dateYearLast = DateTime.Now.AddYears(-1).Year + " /01/01"; dateTimePicker2.Text = dateYearLast; dateTimePicker3.Value = dateTimePicker2.Value.AddYears(1).AddSeconds(-1); break; case "自定义": setTimePickerNull(); break; default: break; } } private void setTimePickerNull() { dateTimePicker2.Format = DateTimePickerFormat.Custom; dateTimePicker2.CustomFormat = " "; dateTimePicker3.Format = DateTimePickerFormat.Custom; dateTimePicker3.CustomFormat = " "; } private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { dateTimePicker2.Format = DateTimePickerFormat.Long; dateTimePicker3.Format = DateTimePickerFormat.Long; } private void dateTimePicker2_ValueChanged(object sender, EventArgs e) { dateTimePicker2.Format = DateTimePickerFormat.Long; beginTime = dateTimePicker2.Value.ToString(); beginTimeText = dateTimePicker2.Text; } private void dateTimePicker3_ValueChanged(object sender, EventArgs e) { dateTimePicker3.Format = DateTimePickerFormat.Long; endTime = dateTimePicker3.Value.ToString(); endTimeText = dateTimePicker3.Text; } } }