using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace Mesnac.Controls.Default
{
    /// <summary>
    /// 时间标签控件
    /// </summary>
    [ToolboxBitmap(typeof(System.Windows.Forms.Timer))]
    public class DateTimeLabel : Label
    {
        private Timer timer;
        private int _interval = 500;
        private string _format = "yyyy-MM-dd HH:mm:ss";

        public DateTimeLabel()
        {
            timer = new Timer();
            timer.Interval = this._interval;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }

        protected void timer_Tick(object sender, EventArgs e)
        {
            if (this.DesignMode == false)
            {
                if (String.IsNullOrEmpty(this._format))
                {
                    this.Text = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
                }
                else
                {
                    try
                    {
                        this.Text = String.Format("{0:" + this._format + "}", DateTime.Now);
                    }
                    catch { }
                }
            }
        }

        /// <summary>
        /// 变化时间间隔
        /// </summary>
        [Description("变化时间间隔"), Category("Appearance")]
        public int Interval
        {
            get { return _interval; }
            set { _interval = value; }
        }

        /// <summary>
        /// 格式
        /// </summary>
        [Description("显示时间的格式"), Category("Appearance")]
        public string Format
        {
            get { return _format; }
            set { _format = value; }
        }
    }
}