vb.net 创建一个简单的计时器来计算秒、分和小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3984831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a simple timer to count seconds, minutes and hours
提问by Kenny Bones
I'm trying to create a pretty simple program that basically is a timer.
I have three sets of labels, lbl_seconds
, lbl_minutes
and lbl_hours
.
These labels have the default value of 00:00
and I want the timer to change that for each label. I have googled this but I cannot seem to find any good info on it.
我正在尝试创建一个非常简单的程序,它基本上是一个计时器。我有三组标签lbl_seconds
,lbl_minutes
和lbl_hours
。这些标签的默认值为 ,00:00
我希望计时器为每个标签更改该值。我用谷歌搜索过这个,但我似乎找不到任何关于它的好信息。
Do I need three separate timers? I have also noticed that the timers have their own tick event handler. I guess it's in this that I need to change the value of the label. How to do just that?
我需要三个独立的计时器吗?我还注意到计时器有自己的滴答事件处理程序。我想正是在这一点上,我需要更改标签的值。如何做到这一点?
回答by MindTwist
Here is an example of this
这是一个例子
Dim timercount As Integer = 60 'The number of seconds
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Interval = 1000 'The number of miliseconds in a second
Timer1.Enabled = True 'Start the timer
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Timer1.Enabled = False 'Stop the timer
timercount = 60 'Reset to 60 seconds
lblOutput.Text = timercount.ToString() 'Reset the output display to 60
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lblOutput.Text = timercount.ToString() 'show the countdown in the label
If timercount = 0 Then 'Check to see if it has reached 0, if yes then stop timer and display done
Timer1.Enabled = False
lblOutput.Text = "Done"
Else 'If timercount is higher then 0 then subtract one from it
timercount -= 1
End If
End Sub
回答by Alex Essilfie
I think you need something of this sort
我想你需要这样的东西
Public Function GetTime(Time as Integer) As String
Dim Hrs As Integer 'number of hours '
Dim Min As Integer 'number of Minutes '
Dim Sec As Integer 'number of Sec '
'Seconds'
Sec = Time Mod 60
'Minutes'
Min = ((Time - Sec) / 60) Mod 60
'Hours'
Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60
Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function
You pass the time (in seconds) you'd like to display on the label's text and the time will be formatted as you like it.
您传递想要在标签文本上显示的时间(以秒为单位),时间将按照您的喜好进行格式化。
e.g.
例如
lblTime.Text = GetTime(90)
This will display 00:01:30
on the label.
这将显示00:01:30
在标签上。
For reference, you can see this projectI submitted on FreeVBCodesome time ago. The only caveat is the project is in VB6. You should be able to open it in Visual Studio though.
可以参考我前段时间在FreeVBCode上提交的这个项目。唯一需要注意的是该项目在 VB6 中。不过,您应该可以在 Visual Studio 中打开它。
回答by AwesomeProgrammingDude
Start off by adding a timer. Call it whatever you like, in this example I will be keeping it as Timer1. Add a label and set the text as: 00:00.
首先添加一个计时器。不管你喜欢怎么称呼它,在这个例子中,我将把它保留为 Timer1。添加标签并将文本设置为:00:00。
In the code after the class has been set (usually it is Public Class Form1) make a variable as a stopwatch: Dim stopwatch As New Stopwatch
在类设置后的代码中(通常是公共类 Form1),将变量作为秒表: Dim stopwatch As New Stopwatch
In the timer tick event code, put the following: (Please note that my 00:00 label is called Label1)
在计时器滴答事件代码中,输入以下内容:(请注意,我的 00:00 标签称为 Label1)
Label1.Text = String.Format("{0}:{1}:{2}", watch.Elapsed.Hours.ToString("00"), watch.Elapsed.Minutes.ToString("00"), watch.Elapsed.Seconds.ToString("00"))
回答by bswietochowski
Use one timer and in event sub change value of your labels.
使用一个计时器,并在事件中更改标签的值。
You need one timer and three counter for seconds, minutes and hours.
您需要一个计时器和三个计数器来显示秒、分和小时。
Count minutes, then modulo minutes / 60, if return 0 then start count minutes.
Modulo minutes/60, if return 0 then start count hours.
计数分钟,然后取模分钟 / 60,如果返回 0 则开始计数分钟。
Modulo 分钟/60,如果返回 0 则开始计数小时。