DropDownTime.cs
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:5k
源码类别:

OA系统

开发平台:

C#

  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. namespace OThinker.H3.WorkSheet
  6. {
  7. public enum DropDownTimeModal
  8. {
  9. Date, 
  10. SimplifiedTime, 
  11. Time
  12. }
  13. /// <summary>
  14. /// 这里最好采用继承的方式,因为DropDownCalendar实现了IPostBackDataHandler接口,。
  15. /// </summary>
  16. [DefaultProperty("Text"), ToolboxData("<{0}:DropDownTime runat=server></{0}:DropDownTime>")]
  17. public class DropDownTime : DropDownCalendar
  18. {
  19. private DropDownTimeModal _Modal = DropDownTimeModal.Time;
  20. [Bindable(true),Category("WorkSheet")]
  21. public DropDownTimeModal Modal
  22. {
  23. get
  24. {
  25. return this._Modal;
  26. }
  27. set
  28. {
  29. this._Modal = value;
  30. switch(this._Modal)
  31. {
  32. case DropDownTimeModal.Date:
  33. this.SimplifiedTimeList.Visible = false;
  34. this.SimplifiedTimeList.SelectedIndex = 0;
  35. this.HourList.SelectedIndex = 0;
  36. this.HourList.Visible = false;
  37. this.MinuteList.SelectedIndex = 0;
  38. this.MinuteList.Visible = false;
  39. this.SecondList.SelectedIndex = 0;
  40. this.SecondList.Visible = false;
  41. break;
  42. case DropDownTimeModal.SimplifiedTime:
  43. this.SimplifiedTimeList.Visible = true;
  44. this.HourList.Visible = false;
  45. this.MinuteList.Visible = false;
  46. this.SecondList.Visible = false;
  47. break;
  48. case DropDownTimeModal.Time:
  49. this.SimplifiedTimeList.Visible = false;
  50. this.HourList.Visible = true;
  51. this.MinuteList.Visible = true;
  52. this.SecondList.Visible = true;
  53. break;
  54. default:
  55. throw new NotImplementedException();
  56. }
  57. }
  58. }
  59. // 简化的时间列表
  60. private DropDownList SimplifiedTimeList = new DropDownList();
  61. // 小时
  62. private DropDownList HourList = new DropDownList();
  63. // 分
  64. private DropDownList MinuteList = new DropDownList();
  65. // 妙
  66. private DropDownList SecondList = new DropDownList();
  67. public DropDownTime()
  68. {
  69. this.Modal = DropDownTimeModal.SimplifiedTime;
  70. base.Size = "7";
  71. for(int h=0; h<10; h++)
  72. {
  73. this.SimplifiedTimeList.Items.Add("0" + h + ":" + "00");
  74. this.SimplifiedTimeList.Items.Add("0" + h + ":" + "30");
  75. }
  76. for(int h=10; h<24; h++)
  77. {
  78. this.SimplifiedTimeList.Items.Add(h + ":" + "00");
  79. this.SimplifiedTimeList.Items.Add(h + ":" + "30");
  80. }
  81. this.Controls.Add(this.SimplifiedTimeList);
  82. for(int count=0; count<24; count++)
  83. {
  84. this.HourList.Items.Add(count.ToString());
  85. }
  86. this.Controls.Add(this.HourList);
  87. for(int count=0; count<60; count++)
  88. {
  89. this.MinuteList.Items.Add(count.ToString());
  90. }
  91. this.Controls.Add(this.MinuteList);
  92. for(int count=0; count<60; count++)
  93. {
  94. this.SecondList.Items.Add(count.ToString());
  95. }
  96. this.Controls.Add(this.SecondList);
  97. }
  98. [Browsable(false)]
  99. public System.DateTime TimeValue
  100. {
  101. get
  102. {
  103. try
  104. {
  105. string date;
  106. switch(this.Modal)
  107. {
  108. case DropDownTimeModal.Date:
  109. return System.DateTime.Parse(base.Text);
  110. case DropDownTimeModal.SimplifiedTime:
  111. date = base.Text + " " + this.SimplifiedTimeList.SelectedValue;
  112. return System.DateTime.Parse(date);
  113. case DropDownTimeModal.Time:
  114. date = base.Text;
  115. int hour = int.Parse(this.HourList.SelectedValue);
  116. int minute = int.Parse(this.MinuteList.SelectedValue);
  117. int second = int.Parse(this.SecondList.SelectedValue);
  118. string time = date + " " + hour + ":" + minute + ":" + second;
  119. return System.DateTime.Parse(time);
  120. default:
  121. throw new NotImplementedException();
  122. }
  123. }
  124. catch
  125. {
  126. return System.DateTime.Now;
  127. }
  128. }
  129. set
  130. {
  131. base.Text = value.ToShortDateString();
  132. switch(this.Modal)
  133. {
  134. case DropDownTimeModal.Date:
  135. break;
  136. case DropDownTimeModal.SimplifiedTime:
  137. // 找到一个最接近的时间
  138. ListItem nearestItem = null;
  139. System.TimeSpan nearestInterval = System.TimeSpan.MaxValue;
  140. foreach(ListItem item in this.SimplifiedTimeList.Items)
  141. {
  142. string date = base.Text + " " + item.Value;
  143. System.DateTime itemDate = System.DateTime.Parse(date);
  144. System.TimeSpan interval;
  145. if(itemDate > value)
  146. {
  147. interval = itemDate.Subtract(value);
  148. }
  149. else
  150. {
  151. interval = value.Subtract(itemDate);
  152. }
  153. if(interval < nearestInterval)
  154. {
  155. nearestInterval = interval;
  156. nearestItem = item;
  157. }
  158. }
  159. // 设置最近的这个值
  160. this.SimplifiedTimeList.SelectedValue = nearestItem.Value;
  161. break;
  162. case DropDownTimeModal.Time:
  163. this.HourList.SelectedValue = value.Hour.ToString();
  164. this.MinuteList.SelectedValue = value.Minute.ToString();
  165. this.SecondList.SelectedValue = value.Second.ToString();
  166. break;
  167. default:
  168. throw new NotImplementedException();
  169. }
  170. }
  171. }
  172. [Browsable(false)]
  173. public string TextValue
  174. {
  175. get
  176. {
  177. return this.TimeValue.ToString();
  178. }
  179. set
  180. {
  181. this.TimeValue = System.DateTime.Parse(value);
  182. }
  183. }
  184. protected override void Render(HtmlTextWriter writer)
  185. {
  186. base.Render (writer);
  187. this.SimplifiedTimeList.RenderControl(writer);
  188. this.HourList.RenderControl(writer);
  189. this.MinuteList.RenderControl(writer);
  190. this.SecondList.RenderControl(writer);
  191. }
  192. }
  193. }