刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(3)-.Net技术-3P代码网
繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 组件控件开发 >> 刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(3)

刚写好的Asp.Net时间和日期的Label控件。作为讲解Asp.net控件开发的第一部分:继承开发(3)

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:6  文字大小:【】【】【
简介://========================================================================== //名称: ZYQ.WebControls.Cultural.DateTimePick.DateTimePickLabel //       Asp.Net服...

//==========================================================================

//名称: 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

///

/// 枚举类。DateTimeFormatInfo的日期和时间的格式类型

/// (Default,ShortDate,LongDate,FullDate,RFC1123,ISO8601,UniversalSortableDateTime,ShortTime,LongTime,MonthDay,YearMonth)

///

public enum DateFormatOption

{

Customer,

ShortDate,

LongDate,

FullDate,

RFC1123,

ISO8601,

UniversalSortableDateTime,

ShortTime,

LongTime,

MonthDay,

YearMonth,

}

#endregion

#region FormatChangeEventArgs

///

/// FormatChangeEventArgs: 继承自System.EventArgs.

/// 格式变化会触发。

///

public class FormatChangeEventArgs: EventArgs

{

#region 私有变量声明

DTFormatSetting _dtSetting;

#endregion

#region 属性

///

/// (get)返回 ZYQ.WebControls.cultural.DateTimePick.DTFormatSetting对象

///

public DTFormatSetting DTSetting

{

get{return this._dtSetting;}

}

#endregion

#region 构造

public FormatChangeEventArgs(DTFormatSetting newSetting)

{

this._dtSetting = newSetting;

}

#endregion

}

#endregion

#region DateTimeFormateEventHandler

///

/// 委托FormatChangeEventArgs

///

public delegate void DateTimeFormateEventHandler(object Sender,FormatChangeEventArgs e);

#endregion

#region DTFormatSetting

///

/// 自定义的时间和日期的格式

///

[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 属性

///

/// RFC 1766 name。

/// 由此属性可创建ZYQ.zyqCultrueInfo对象。

/// 变更此属性会触发DateTimeFormatChanged事件。

///

[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);

}

}

///

/// 自定义的格式字符串。如:“yy/MM/dd”。

/// 变更此属性会触发DateTimeFormatChanged事件。

///

[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 构造

///

/// 创建DTFormatSetting对象。

///

public DTFormatSetting()

{

this._languageCode = CultureInfo.CurrentCulture.Name;

this._datetimeFormat = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;

FormatChangeEventArgs e = new FormatChangeEventArgs(this);

this.OnDateFormatChanged(e);

}

///

/// 通过System.Globalization.CultureInfo及子类创建DTFormatSetting对象。

///

/// System.Globalization.CultureInfo及子类

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);

}

///

/// 通过语言国家码创建DTFormatSetting对象。

///

/// 语言国家码(如: zh-CN)

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);

}

///

/// 创建DTFormatSetting对象.

///

/// 格式化的时间日期的字符串("yyyy/MM/dd HH:mm:ss tt")

/// 语言国家码(如: zh-CN)

public DTFormatSetting(string dtf,string language)

{

this._languageCode =language;

this._datetimeFormat = dtf;

FormatChangeEventArgs e = new FormatChangeEventArgs(this);

this.OnDateFormatChanged(e);

}

#endregion

#region 受保护的事件处理方法

///

/// DateTimeFormatChanged事件的处理。

///

/// FormatChangeEventArgs

protected virtual void OnDateFormatChanged(FormatChangeEventArgs eventArgument)

{

if(this.DateTimeFormatChanged !=null)

{

this.DateTimeFormatChanged(this,eventArgument);

}

}

#endregion

}

#endregion

#region DTFormatSettingConvert

///

/// 实现DTFormatSetting类型和String类型的双向转换。继承自ExpandableObjectConverter

///

public class DTFormatSettingConverter:ExpandableObjectConverter

{

#region 构造

///

///创建DTFormatSettingConverter类。

///

public DTFormatSettingConverter()

{

}

#endregion

#region 私有辅助方法

///

/// 将DTFormatSetting转化为String类型。

///

/// DTFormatSetting

/// String

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);

}

///

/// 将String转化为DTFormatSetting类型。

///

/// String

/// DTFormatSetting

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

}

责任编辑:admin
相关文章