TimePickerWithCombo.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace UAS_MES_NEW.CustomControl.TimePickerWithCombo
  11. {
  12. public partial class TimePickerWithCombo : UserControl
  13. {
  14. public TimePickerWithCombo()
  15. {
  16. InitializeComponent();
  17. setTimePickerNull();
  18. }
  19. private string beginTime;
  20. private string endTime;
  21. private string beginTimeText;
  22. private string endTimeText;
  23. public string BeginTime { get { return beginTime; } set { beginTime = value; } }
  24. public string EndTime { get { return endTime; } set { endTime = value; } }
  25. public string BeginTimeText
  26. {
  27. get
  28. {
  29. return beginTimeText;
  30. }
  31. set
  32. {
  33. beginTimeText = value;
  34. }
  35. }
  36. public string EndTimeText
  37. {
  38. get
  39. {
  40. return endTimeText;
  41. }
  42. set
  43. {
  44. endTimeText = value;
  45. }
  46. }
  47. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  48. {
  49. string time = comboBox1.SelectedItem.ToString();
  50. string beginTime = "";
  51. string endTime = "";
  52. switch (time)
  53. {
  54. case "今天":
  55. dateTimePicker2.Value = DateTime.Now.Date;
  56. dateTimePicker3.Value = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
  57. beginTime = dateTimePicker2.Value.ToString();
  58. endTime = dateTimePicker3.Value.ToString();
  59. break;
  60. case "昨天":
  61. dateTimePicker2.Value = DateTime.Now.AddDays(-1).Date;
  62. dateTimePicker3.Value = DateTime.Now.Date.AddSeconds(-1);
  63. beginTime = dateTimePicker2.Value.ToString();
  64. endTime = dateTimePicker3.Value.ToString();
  65. break;
  66. case "本月":
  67. string dateMonthNow = DateTime.Now.Year + " " + DateTime.Now.Month + " 01";
  68. dateTimePicker2.Text = dateMonthNow;
  69. dateTimePicker3.Value = dateTimePicker2.Value.AddMonths(1).AddSeconds(-1);
  70. break;
  71. case "上个月":
  72. string dateMonthLast = DateTime.Now.Year + " " + DateTime.Now.AddMonths(-1).Month + " 01";
  73. dateTimePicker2.Text = dateMonthLast;
  74. dateTimePicker3.Value = dateTimePicker2.Value.AddMonths(1).AddSeconds(-1);
  75. break;
  76. case "本年度":
  77. string dateYearNow = DateTime.Now.Year + " /01/01";
  78. dateTimePicker2.Text = dateYearNow;
  79. dateTimePicker3.Value = dateTimePicker2.Value.AddYears(1).AddSeconds(-1);
  80. break;
  81. case "上年度":
  82. string dateYearLast = DateTime.Now.AddYears(-1).Year + " /01/01";
  83. dateTimePicker2.Text = dateYearLast;
  84. dateTimePicker3.Value = dateTimePicker2.Value.AddYears(1).AddSeconds(-1);
  85. break;
  86. case "自定义":
  87. setTimePickerNull();
  88. break;
  89. default:
  90. break;
  91. }
  92. }
  93. private void setTimePickerNull()
  94. {
  95. dateTimePicker2.Format = DateTimePickerFormat.Custom;
  96. dateTimePicker2.CustomFormat = " ";
  97. dateTimePicker3.Format = DateTimePickerFormat.Custom;
  98. dateTimePicker3.CustomFormat = " ";
  99. }
  100. private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
  101. {
  102. dateTimePicker2.Format = DateTimePickerFormat.Long;
  103. dateTimePicker3.Format = DateTimePickerFormat.Long;
  104. }
  105. private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
  106. {
  107. dateTimePicker2.Format = DateTimePickerFormat.Long;
  108. beginTime = dateTimePicker2.Value.ToString();
  109. beginTimeText = dateTimePicker2.Text;
  110. }
  111. private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
  112. {
  113. dateTimePicker3.Format = DateTimePickerFormat.Long;
  114. endTime = dateTimePicker3.Value.ToString();
  115. endTimeText = dateTimePicker3.Text;
  116. }
  117. }
  118. }