首页 > 编程 > .NET > 正文

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

2024-07-10 13:03:56
字体:
来源:转载
供稿:网友

最大的网站源码资源下载站,

//==========================================================================
//名称: zyq.webcontrols.cultural.datetimepick.datetimepicklabel
//       asp.net服务控件
//版本: 1.0.0.0
//作者: 张宇庆
//日期: 2003.2.12
//email: [email protected]
//说明: 本控件及源代码只是为《计算机世界》开发者俱乐部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

}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表