DropDownTime.cs
资源名称:H3_OA.rar [点击查看]
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:5k
源码类别:
OA系统
开发平台:
C#
- using System;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.ComponentModel;
- namespace OThinker.H3.WorkSheet
- {
- public enum DropDownTimeModal
- {
- Date,
- SimplifiedTime,
- Time
- }
- /// <summary>
- /// 这里最好采用继承的方式,因为DropDownCalendar实现了IPostBackDataHandler接口,。
- /// </summary>
- [DefaultProperty("Text"), ToolboxData("<{0}:DropDownTime runat=server></{0}:DropDownTime>")]
- public class DropDownTime : DropDownCalendar
- {
- private DropDownTimeModal _Modal = DropDownTimeModal.Time;
- [Bindable(true),Category("WorkSheet")]
- public DropDownTimeModal Modal
- {
- get
- {
- return this._Modal;
- }
- set
- {
- this._Modal = value;
- switch(this._Modal)
- {
- case DropDownTimeModal.Date:
- this.SimplifiedTimeList.Visible = false;
- this.SimplifiedTimeList.SelectedIndex = 0;
- this.HourList.SelectedIndex = 0;
- this.HourList.Visible = false;
- this.MinuteList.SelectedIndex = 0;
- this.MinuteList.Visible = false;
- this.SecondList.SelectedIndex = 0;
- this.SecondList.Visible = false;
- break;
- case DropDownTimeModal.SimplifiedTime:
- this.SimplifiedTimeList.Visible = true;
- this.HourList.Visible = false;
- this.MinuteList.Visible = false;
- this.SecondList.Visible = false;
- break;
- case DropDownTimeModal.Time:
- this.SimplifiedTimeList.Visible = false;
- this.HourList.Visible = true;
- this.MinuteList.Visible = true;
- this.SecondList.Visible = true;
- break;
- default:
- throw new NotImplementedException();
- }
- }
- }
- // 简化的时间列表
- private DropDownList SimplifiedTimeList = new DropDownList();
- // 小时
- private DropDownList HourList = new DropDownList();
- // 分
- private DropDownList MinuteList = new DropDownList();
- // 妙
- private DropDownList SecondList = new DropDownList();
- public DropDownTime()
- {
- this.Modal = DropDownTimeModal.SimplifiedTime;
- base.Size = "7";
- for(int h=0; h<10; h++)
- {
- this.SimplifiedTimeList.Items.Add("0" + h + ":" + "00");
- this.SimplifiedTimeList.Items.Add("0" + h + ":" + "30");
- }
- for(int h=10; h<24; h++)
- {
- this.SimplifiedTimeList.Items.Add(h + ":" + "00");
- this.SimplifiedTimeList.Items.Add(h + ":" + "30");
- }
- this.Controls.Add(this.SimplifiedTimeList);
- for(int count=0; count<24; count++)
- {
- this.HourList.Items.Add(count.ToString());
- }
- this.Controls.Add(this.HourList);
- for(int count=0; count<60; count++)
- {
- this.MinuteList.Items.Add(count.ToString());
- }
- this.Controls.Add(this.MinuteList);
- for(int count=0; count<60; count++)
- {
- this.SecondList.Items.Add(count.ToString());
- }
- this.Controls.Add(this.SecondList);
- }
- [Browsable(false)]
- public System.DateTime TimeValue
- {
- get
- {
- try
- {
- string date;
- switch(this.Modal)
- {
- case DropDownTimeModal.Date:
- return System.DateTime.Parse(base.Text);
- case DropDownTimeModal.SimplifiedTime:
- date = base.Text + " " + this.SimplifiedTimeList.SelectedValue;
- return System.DateTime.Parse(date);
- case DropDownTimeModal.Time:
- date = base.Text;
- int hour = int.Parse(this.HourList.SelectedValue);
- int minute = int.Parse(this.MinuteList.SelectedValue);
- int second = int.Parse(this.SecondList.SelectedValue);
- string time = date + " " + hour + ":" + minute + ":" + second;
- return System.DateTime.Parse(time);
- default:
- throw new NotImplementedException();
- }
- }
- catch
- {
- return System.DateTime.Now;
- }
- }
- set
- {
- base.Text = value.ToShortDateString();
- switch(this.Modal)
- {
- case DropDownTimeModal.Date:
- break;
- case DropDownTimeModal.SimplifiedTime:
- // 找到一个最接近的时间
- ListItem nearestItem = null;
- System.TimeSpan nearestInterval = System.TimeSpan.MaxValue;
- foreach(ListItem item in this.SimplifiedTimeList.Items)
- {
- string date = base.Text + " " + item.Value;
- System.DateTime itemDate = System.DateTime.Parse(date);
- System.TimeSpan interval;
- if(itemDate > value)
- {
- interval = itemDate.Subtract(value);
- }
- else
- {
- interval = value.Subtract(itemDate);
- }
- if(interval < nearestInterval)
- {
- nearestInterval = interval;
- nearestItem = item;
- }
- }
- // 设置最近的这个值
- this.SimplifiedTimeList.SelectedValue = nearestItem.Value;
- break;
- case DropDownTimeModal.Time:
- this.HourList.SelectedValue = value.Hour.ToString();
- this.MinuteList.SelectedValue = value.Minute.ToString();
- this.SecondList.SelectedValue = value.Second.ToString();
- break;
- default:
- throw new NotImplementedException();
- }
- }
- }
- [Browsable(false)]
- public string TextValue
- {
- get
- {
- return this.TimeValue.ToString();
- }
- set
- {
- this.TimeValue = System.DateTime.Parse(value);
- }
- }
- protected override void Render(HtmlTextWriter writer)
- {
- base.Render (writer);
- this.SimplifiedTimeList.RenderControl(writer);
- this.HourList.RenderControl(writer);
- this.MinuteList.RenderControl(writer);
- this.SecondList.RenderControl(writer);
- }
- }
- }