刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(3) - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(3) (http://www.webasp.net/article/5/4127.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-12 |
| //========================================================================== //名称: ZYQ.WebControls.Cultural.DateTimePick.DateTimePickLabel // Asp.Net服务控件 //版本: 1.0.0.0 //作者: 张宇庆 //日期: 2003.2.12 //Email: raxzhang@sina.com //说明: 本控件及源代码只是为《计算机世界》开发者俱乐部Asp.Net论坛学习如何开发Asp.net // 服务器端控件而开发。未经本人同意请勿用作商业用途。 // //========================================================================== using System; using System.Drawing.Design; using System.Globalization; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using ZYQ; namespace ZYQ.WebControls.Cultural.DateTimePick { #region DateFormatOption /// <summary> /// 枚举类。DateTimeFormatInfo的日期和时间的格式类型 /// (Default,ShortDate,LongDate,FullDate,RFC1123,ISO8601,UniversalSortableDateTime,ShortTime,LongTime,MonthDay,YearMonth) /// </summary> public enum DateFormatOption { Customer, ShortDate, LongDate, FullDate, RFC1123, ISO8601, UniversalSortableDateTime, ShortTime, LongTime, MonthDay, YearMonth, } #endregion #region FormatChangeEventArgs /// <summary> /// FormatChangeEventArgs: 继承自System.EventArgs. /// 格式变化会触发。 /// </summary> public class FormatChangeEventArgs: EventArgs { #region 私有变量声明 DTFormatSetting _dtSetting; #endregion #region 属性 /// <summary> /// (get)返回 ZYQ.WebControls.cultural.DateTimePick.DTFormatSetting对象 /// </summary> public DTFormatSetting DTSetting { get{return this._dtSetting;} } #endregion #region 构造 public FormatChangeEventArgs(DTFormatSetting newSetting) { this._dtSetting = newSetting; } #endregion } #endregion #region DateTimeFormateEventHandler /// <summary> /// 委托FormatChangeEventArgs /// </summary> public delegate void DateTimeFormateEventHandler(object Sender,FormatChangeEventArgs e); #endregion #region DTFormatSetting /// <summary> /// 自定义的时间和日期的格式 /// </summary> [Editor(typeof(ZYQ.WebControls.Cultural.Design.DateTimePickDesign),typeof(UITypeEditor))] public class DTFormatSetting { #region 私有变量声明 private string _datetimeFormat; //设置时间显示的格式,类初始化时会指定一个初始格式 private string _languageCode; //语言代码 public event DateTimeFormateEventHandler DateTimeFormatChanged; //声明事件 #endregion #region 属性 /// <summary> /// RFC 1766 name。 /// 由此属性可创建ZYQ.zyqCultrueInfo对象。 /// 变更此属性会触发DateTimeFormatChanged事件。 /// </summary> [NotifyParentProperty(true),Browsable(true),Category("Appearance"),Description("设置和获取选定国家和语言的RFC 1766 name。默认为:zh-CN 中文(中华人民共和国)"),DefaultValue("zh-cn")] public string LanguageAndCountry { get { return this._languageCode.ToLower(); } set { this._languageCode =value; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } } /// <summary> /// 自定义的格式字符串。如:“yy/MM/dd”。 /// 变更此属性会触发DateTimeFormatChanged事件。 /// </summary> [NotifyParentProperty(true),Browsable(true),Category("Appearance"),DefaultValue("yyyy'年'M'月'd'日' H:mm:ss"),Description("在当前CultrueInfo允许的范围中,可自定义时间和日期的显示格式。")] public string FormateString { get{return this._datetimeFormat;} set { this._datetimeFormat=value; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } } #endregion #region 构造 /// <summary> /// 创建DTFormatSetting对象。 /// </summary> public DTFormatSetting() { this._languageCode = CultureInfo.CurrentCulture.Name; this._datetimeFormat = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } /// <summary> /// 通过System.Globalization.CultureInfo及子类创建DTFormatSetting对象。 /// </summary> /// <param name="cul">System.Globalization.CultureInfo及子类</param> public DTFormatSetting(CultureInfo cul) { zyqCultrueInfo myInfo = new zyqCultrueInfo(cul.Name,true); this._languageCode = cul.Name; this._datetimeFormat =myInfo.DateTimeFormat.FullDateTimePattern; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } /// <summary> /// 通过语言国家码创建DTFormatSetting对象。 /// </summary> /// <param name="languageName">语言国家码(如: zh-CN)</param> public DTFormatSetting(string language) { this._languageCode = language; zyqCultrueInfo myInfo = new zyqCultrueInfo(language,true); this._datetimeFormat =myInfo.DateTimeFormat.FullDateTimePattern; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } /// <summary> /// 创建DTFormatSetting对象. /// </summary> /// <param name="dtf">格式化的时间日期的字符串("yyyy/MM/dd HH:mm:ss tt")</param> /// <param name="language">语言国家码(如: zh-CN)</param> public DTFormatSetting(string dtf,string language) { this._languageCode =language; this._datetimeFormat = dtf; FormatChangeEventArgs e = new FormatChangeEventArgs(this); this.OnDateFormatChanged(e); } #endregion #region 受保护的事件处理方法 /// <summary> /// DateTimeFormatChanged事件的处理。 /// </summary> /// <param name="eventArgument">FormatChangeEventArgs</param> protected virtual void OnDateFormatChanged(FormatChangeEventArgs eventArgument) { if(this.DateTimeFormatChanged !=null) { this.DateTimeFormatChanged(this,eventArgument); } } #endregion } #endregion #region DTFormatSettingConvert /// <summary> /// 实现DTFormatSetting类型和String类型的双向转换。继承自ExpandableObjectConverter /// </summary> public class DTFormatSettingConverter:ExpandableObjectConverter { #region 构造 /// <summary> ///创建DTFormatSettingConverter类。 /// </summary> public DTFormatSettingConverter() { } #endregion #region 私有辅助方法 /// <summary> /// 将DTFormatSetting转化为String类型。 /// </summary> /// <param name="obj">DTFormatSetting</param> /// <returns>String</returns> private string ToString(object obj) { DTFormatSetting formatting= obj as DTFormatSetting; return string.Format("{0},{1}",formatting.FormateString,formatting.LanguageAndCountry);//{2},{3},{4},{5}",formatting.FormateString,formatting.LanguageAndCountry,formatting.DateSeparator,formatting.TimeSeparator,formatting.AMDesignator,formatting.PMDesignator); } /// <summary> /// 将String转化为DTFormatSetting类型。 /// </summary> /// <param name="obj">String</param> /// <returns>DTFormatSetting</returns> private DTFormatSetting FromString(object obj) { string[] values=((string)obj).Split(','); if(values.Length !=2) throw new Exception("设置值无法转换"); try { return new DTFormatSetting(values[0],values[1]); } catch { throw new Exception("设置值无法转换"); } } #endregion #region 重写TypeConvert类的相关方法和函数 public override bool CanConvertFrom(ITypeDescriptorContext context,Type sourceType) { if(sourceType ==typeof(string)) return true; return base.CanConvertFrom(context,sourceType); } public override object ConvertFrom(ITypeDescriptorContext context,CultureInfo culture,object value) { if(value==typeof(string)) return FromString(value); return base.ConvertFrom(context,culture,value); } public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType) { if(destinationType ==typeof(DTFormatSetting)) return true; return base.CanConvertTo(context,destinationType); } public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destinationType) { if((destinationType == typeof(string))&& value is DTFormatSetting) return ToString(value); return base.ConvertTo(context,culture,value,destinationType); } #endregion } #endregion } |
| webasp.net |