自定义控件的事件

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

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


}
}
...全文
168 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
罪人不釋之枷 2013-07-08
  • 打赏
  • 举报
回复
你是在有效的域裏面new的Timer么?
kxyzjm62 2013-07-01
  • 打赏
  • 举报
回复
在控件调试器中执行时,当执行到“Clocks_TimerEvent (this, e);”显示“未将对象引用设置到对象的实例。”错误,
kxyzjm62 2013-07-01
  • 打赏
  • 举报
回复
在主程序裏面直接運行 C# code ? 1 initTimer(); 就好 请详细解释,谢谢
kxyzjm62 2013-07-01
  • 打赏
  • 举报
回复
在控件调试器中执行时,当执行到“Clocks_TimerEvent (this, e);”显示“未将对象引用设置到对象的实例。”错误
罪人不釋之枷 2013-07-01
  • 打赏
  • 举报
回复
在主程序裏面直接運行
initTimer();
就好
罪人不釋之枷 2013-07-01
  • 打赏
  • 举报
回复
        //計時器
        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)
            {……}
        }
罪人不釋之枷 2013-07-01
  • 打赏
  • 举报
回复
這是我的代碼,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)
            {……}
kxyzjm62 2013-07-01
  • 打赏
  • 举报
回复
谢谢二位的回答,我修改了一下,代码如下: if (DateTime.Compare(XTS ,Convert.ToDateTime(DTimer))>0) { TimerE = false; Clocks_TimerEvent (this, e); } 目前,事件可以引发了,但是,将控件放到程序中使用时,在引发事件后,就出错了。 请指导
kxyzjm62 2013-07-01
  • 打赏
  • 举报
回复
谢谢而为的回答,我修改了一下,代码如下: if (DateTime.Compare(XTS ,Convert.ToDateTime(DTimer))>0) { TimerE = false; Clocks_TimerEvent (this, e); } 目前,时间可以引发了,但是,将控件放到程序中使用时,在引发事件后,就出错了。 请指导
望尽星辰 2013-07-01
  • 打赏
  • 举报
回复
不触发的可能是1、设置的时间间隔太长,你根本就没等到;2、控件你未启用,所以一直没有运行,你调用START()方法试试
  • 打赏
  • 举报
回复
timer1_Tick是哪个timer的事件,这个timer的interval是多少?不要定的太大。 if (XTS == Convert.ToDateTime(DTimer)),不要定这么死,结合interval,最好在一定范围内触发。否则永远触发不了。

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧