在 vb 中使用定时器 [英] Using timers in vb
问题描述
我不明白如何在 vb.net 中使用计时器我想制作一个简单的程序,当我按下按钮时,计时器启动,标签每秒更改一次,直到 60 秒过去.我想我应该把它放在按钮事件中
I do not understand how to utilize the timers in vb.net I want to make a simple program where when I press a button the timer starts and the label changes it's number every second until 60 seconds have passed. I think I should put this in the button event
Timer1.Start()
但我不确定从那里开始做什么.我该怎么做?
But I am unsure of what to do from there. How do I go about doing this?
推荐答案
好吧 Timer1.Start()
启动计时器,但您需要声明计时器滴答的频率.
Well Timer1.Start()
starts the timer, but you need to declare how often the timer ticks.
Timer1.Interval = 1000
将使计时器每 1000 毫秒或 1 秒滴答一次.您希望为计时器执行的操作位于 Timer_Tick
事件处理程序中.
will make the timer tick every 1000 miliseconds, or 1 sec. The actions that you want to happen for the timer go in the Timer_Tick
event handler.
为了允许标签递增,您可以使用全局变量:
In order to allow the label to increment you could use a global variable:
Public Class MainBox
Dim counter As Int
Private Sub Form_Load(sender As System.Object, e As System.EventArgs)
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) HandlesTimer1.Tick<action>
counter = counter + 1
label1.Text = counter
End Sub
这篇关于在 vb 中使用定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!