首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

自定义控件的事件解决方案

2013-07-08 
自定义控件的事件最近在学习自定义控件,参考网上的文章和代码写了一个控件,希望在达到定时时间是引发一个

自定义控件的事件
最近在学习自定义控件,参考网上的文章和代码写了一个控件,希望在达到定时时间是引发一个事件,现在的问题是:到时间后,事件不能引发。代码如下:

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

//
using System.Timers;

//

namespace WindowsFormsControlLibrary1

{
    [ToolboxBitmap(typeof(System.Windows.Forms .Timer))]

    public partial class Clocks : UserControl
    {
        public event EventHandler Clocks_TimerEvent;//定义事件



        private DateTime Time;
        private Color colFColor;
        private Color colBColor;
        public Pen HPn;
        public Pen MPn;
        public Pen SPn;

        public int HW;
        public int MW;
        public int SW;

        public Boolean BL;
        public String  DT;
        public DateTime XTS;

        public Image FImageFil;

        public Color ClockFColor;
        //public Color ClockBColor;
        public Clocks()
        {
            InitializeComponent();
            this.HourColor = Color.Red;//给属性赋初值
            this.MinoutColor =Color .Blue ;
            this.SecondColor  = Color.Black;
            this.HW =1;
            BL = false;
            DT ="1900-02-21 12:23:33";


;

        }
        //添加属性
        //时针颜色
        [DefaultValue("Black"), Description("分针颜色"), Category("Appearance")] //Behavior在行为分类,Appearance在字体分类WindowStyle、Layout、Format、Action 百度查找CategoryAttribute 类
        public Color MinoutColor
        {
            get
            {
                return colBColor;
            }
            set
            {
                colBColor = value;
            }
        }


        //分针颜色
        [DefaultValue("Black"), Description("时针颜色"), Category("Appearance")]
        public Color HourColor
        {
            get
            {
                return colFColor;
            }
            set
            {
                colFColor = value;
            }
        }
        //
        [DefaultValue("Black"), Description("秒针颜色"), Category("Appearance")]
        //DefaultValue的作用:不是提供设计时的默认值,他的作用是 当你在IDE的属性栏里 输入或选择的值 与 你DefaultValue相同时,这个值不变黑【加粗显示】
        public Color SecondColor
        {
            get


            {
                return ClockFColor;
            }
            set
            {
                ClockFColor = value;
            }
        }

        //时针宽度属性
        [DefaultValue("1"), Description("时针宽度"), Category("Appearance")]
        public int HourWide
        {
            get 
            {
                return HW;
            }
             set 
            {
                if (value > 1 && value < 16) //限定时针宽度的取值范围
                   HW = value;
               else
                   HW = 1;
            }
        }

        //
        //添加图像属性
        [DefaultValue(" "), Description("指定图像文件"), Category("Appearance")]
        public Image ImageFile
        {
            get
            {
                return FImageFil;
            }


            set
            {
                FImageFil = value;
            }

        }

        //添加文件属性
        //添加一个是否启动定时的属性
        [DefaultValue("False"), Description("设置是否启动定时"), Category("Appearance")]
        public bool  TimerE
        {
            get
            {
                return BL;
            }
            set
            {
                BL = value;
            }

        }
        //
        //添加一个事件的属性
        [DefaultValue("12:00:00"), Description("设置定时时间"), Category("Appearance")]
        public String  DTimer
        {
            get
            {
                return DT;
            }
            set 
            {
                DT = value;
            }
        }
  
        //
        private void timer1_Tick(object sender, EventArgs e)
        {


            this.Time = DateTime.Now;
            this.Refresh(); //刷新界面,是表针走动
            //定时事件
            if (TimerE)
            {
                XTS = System.DateTime.Now;


              if (XTS  == Convert.ToDateTime(DTimer))
                {
                   Clocks_TimerEvent (this, e);


                }

            }

              

        }

        private void UserClock_Paint(object sender, PaintEventArgs e)
        {
            Graphics dc = e.Graphics;
            Pen pn = new Pen(ForeColor);
            this.HPn = new Pen(HourColor,HW );
            this.MPn = new Pen(MinoutColor);
            this.SPn = new Pen(SecondColor);

            SolidBrush br = new SolidBrush(ForeColor);
            InitCoordinates(dc); //设置回话坐标变换
            DrawDots(dc, br);   //画点
            DrawHourHand(dc, pn); //画时针
            DrawMinuteHand(dc, pn);//画分针
            DrawSecondHand(dc, pn); //画秒针

        }


        public void InitCoordinates(Graphics dc)
        {
            if (this.Width == 0 || this.Height == 0) return; //如果空间的宽高有一个为零,就返回
            dc.TranslateTransform(this.Width / 2, this.Height / 2);//将控件的坐标原点设置为控件中心
            dc.ScaleTransform(this.Width / 250F, this.Height / 250F);//调整比例大小

        }
        public void DrawDots(Graphics dc, Brush brush)
        {
            int iSize=5;
            for (int i = 0; i <= 59; i++)
            {
                if (i % 5 == 0)
                    iSize = 15;
                else
                    iSize = 5;
                dc.FillEllipse(brush, -iSize / 2, -100 - iSize / 2, iSize, iSize);//画圆点
               dc.RotateTransform(6);//旋转6度
            }

        }
        protected virtual   void DrawHourHand(Graphics grfx,Pen pn)
        {
            GraphicsState gs=grfx.Save();
            grfx.RotateTransform(360.0F * Time.Hour / 12 + 30.0F * Time.Minute / 60);
            grfx.DrawLine(this.HPn , 0, 0, 0, -50);
            grfx .Restore(gs);


        }
        protected virtual void DrawMinuteHand(Graphics grfx, Pen pn)
        {
            GraphicsState gs = grfx.Save();
            grfx.RotateTransform(360.0F * Time.Minute / 60 + 6.0F * Time.Second / 60);
            grfx.DrawLine(this.MPn , 0, 0, 0, -70);
            grfx.Restore(gs);
        }
        protected virtual void DrawSecondHand(Graphics grfx, Pen pn)
        {
            GraphicsState gs = grfx.Save();
            grfx.RotateTransform(360.0F * Time.Second / 60 );
            grfx.DrawLine(SPn , 0, 0, 0, -100);
            grfx.Restore(gs);
        }


    }
}

[解决办法]
timer1_Tick是哪个timer的事件,这个timer的interval是多少?不要定的太大。

if (XTS  == Convert.ToDateTime(DTimer)),不要定这么死,结合interval,最好在一定范围内触发。否则永远触发不了。
[解决办法]
不触发的可能是1、设置的时间间隔太长,你根本就没等到;2、控件你未启用,所以一直没有运行,你调用START()方法试试
[解决办法]
這是我的代碼,lZ可以參考下

 
       //計時器
        public static System.Timers.Timer NT = new System.Timers.Timer();

       //設定計時器
        private void initTimer()


        {
            NT.Enabled = true;//否执行System.Timers.Timer.Elapsed事件
            NT.Interval = 1000;//間隔
            //NT.Start();
            NT.Elapsed += new System.Timers.ElapsedEventHandler(NT_Elapsed);//到達時間執行事件
            NT.AutoReset = true;//一直執行
            //NT.AutoReset = false;
        }
//事件
        private void NT_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //獲取hour minute second 等於某值時執行程序
            int intHour = System.DateTime.Now.Hour;
            int intMinute = System.DateTime.Now.Minute;
            int intSecond = System.DateTime.Now.Second;
            
              //每個小時的30:00刷新數據
            if (intMinute == 30 && intSecond == 0)
            {……}

热点排行