怎样使用timer控件

怎样使用timer控件,第1张

1、在winform项目中,打开工具箱,找到timer控件

2、把timer控件拖动到winform窗体中,窗体下方会自动出现时间控件的实例,默认名称是timer1;

3、右击timer1,在菜单中选择“属性”来打开属性窗口;

4、在属性窗口中可以看到timer1的属性;

5、其中Enabled属性表示timer是否可用,如果设置为true,则项目运行后,timer会自动启用;interval属性表示时间控件执行事件的时间间隔。要注意的是interval的单位是毫秒。

做一个显示系统时间的程序吧:窗体form1内添加控件timer1,label1,button1。

双击form1来编写form1_Load(arg0, arg1)事件:

private void form1_Load(object sender, EventArgs e)

{

this.label1.Text = "本程序用于显示当前系统时间"

this.button1.Text = "开始"

}

双击button1来编写button1_Click(arg0, arg1)事件:

private void button1_Click(object sender, EventArgs e)

{

int thisTime = 0

if(this.button1.Text == "开始"){

this.timer1.Interval = 1000

this.timer1.Enabled = true

this.button1.Text = "停止"

}

if(this.button1.Text == "停止"){

this.timer1.Enabled = false

this.button1.Text = "开始"

}

}

双击timer1来编写timer1_Tick(arg0, arg1)事件:

private void timer1_Tick(object sender, EventArgs e)

{

this.label1.Text = "当前系统时间是:" + DateTime.Now.ToString()

}

运行下看看。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11600740.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存