问题描述
我正在尝试使用计时器从我选择的指定时间开始倒数,并使用格式 MM:SS 将时间分为分钟和秒,然后在时间达到 00:00 。
到目前为止,我已经使用了此处找到的先前答案,并据我所知对其进行了修改(计数)倒计时,尽管我遇到了一个障碍,当计时器成功开始倒计时时,倒计时会延迟并且不同步。
例如,倒计时从120秒开始;
02:00
>
02:59
>
02:58
>
02:57
>
02:56
>
02:55
然后在相同测试下继续倒计时90秒以上;
02:30
>
01:29
>
01:28
>
01:27
>
01:26
>
01:25
当倒计时达到 00 或 30 秒时,它错误地显示了剩余的分钟数,并且可以
这是我的计数计时器代码;
Private Sub tmrCountdown_Tick(ByVal sender As System.Object,_
ByVal e As System.EventArgs)_
处理tmrCountdown.Tick
SetTime = SetTime-1
lblTime.Text = FormatTime(SetTime)
如果SetTime = 0则
tmrCountdown.Enabled = False
如果
End Sub
这是我的函数格式化时间的代码;
公共函数FormatTime(ByVal时间为整数)为字符串
Dim Min为整数$ b $ b Dim Sec为整数
'分钟
分钟=((时间-秒)/ 60)Mod 60
'第二
秒=时间Mod 60
返回格式(最小值, 00)& :&格式(秒, 00)
结束函数
这是我的表单加载代码;
Private Sub frmSinglePlayer_Load(ByVal sender As System.Object,_
ByVal e As System.EventArgs)_
处理MyBase.Load
'设置时间。
SetTime = 120
lblTime.Text = FormatTime(SetTime)
tmrCountdown.Enabled =真
结束子
我已经设置;
昏暗SetTime为整数
在我的公开课的顶部,因此我可以将指定的时间输入倒数计时器。
任何帮助都将不胜感激,请记住,我是编程和编程的初学者,容易与大型代码墙混淆。 (我几乎无法理解它的功能。)
感谢您的帮助!
玩这个:
公共类frmSinglePlayer
私有TargetDT作为DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub frmSinglePlayer_Load(发送者为System.Object,作为System.EventArgs)处理MyBase.Load
tmrCountdown.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrCountdown.Start()
End Sub
Private Sub tmrCountdown_Tick(发送者为对象,e为系统.EventArgs)处理tmrCountdown.Tick
Dim ts为TimeSpan = TargetDT.Subtract(DateTime.Now)
如果ts.TotalMilliseconds> 0然后
lblTime.Text = ts.ToString( mm\:ss)
其他
lblTime.Text = 00:00
tmrCountdown.Stop()
MessageBox.Show( Done)
如果
End Sub
End Class
I'm trying to use a timer to count down from a specified time I choose with the time being separated into minutes and seconds using the format MM:SS and then stop when the time reaches 00:00.
So far I've used a previous answer that was found on here and modified it to the best of my knowledge with counting down although I've hit a snag in which when the timer successfully starts counting down, it's delayed and out of sync when counting down the minutes.
For example, counting down from 120 seconds;
02:00
>
02:59
>
02:58
>
02:57
>
02:56
>
02:55
And then when continuing to count down past 90 seconds under the same test;
02:30
>
01:29
>
01:28
>
01:27
>
01:26
>
01:25
When the countdown reaches 00 or 30 seconds, it incorrectly displays the minutes left and can't understand or figure out how to fix it.
Here is my code for my Counting Timer;
Private Sub tmrCountdown_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles tmrCountdown.Tick
SetTime = SetTime - 1
lblTime.Text = FormatTime(SetTime)
If SetTime = 0 Then
tmrCountdown.Enabled = False
End If
End Sub
Here is my code for the Function formatting the time;
Public Function FormatTime(ByVal Time As Integer) As String
Dim Min As Integer
Dim Sec As Integer
'Minutes
Min = ((Time - Sec) / 60) Mod 60
'Seconds
Sec = Time Mod 60
Return Format(Min, "00") & ":" & Format(Sec, "00")
End Function
And here is my code for the Form Load;
Private Sub frmSinglePlayer_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
'Setting the time.
SetTime = 120
lblTime.Text = FormatTime(SetTime)
tmrCountdown.Enabled = True
End Sub
I've set;
Dim SetTime As Integer
At the top of my Public Class so I am able to input a specified time into the countdown timer. This is probably something incredibly silly and I can't figure out what it is.
Any help is greatly appreciated and please bare in mind, I am a beginner at programming and get easily confused with large walls of code. (I can barely understand the Function as it is.)
Thank you for helping!
Play with this:
Public Class frmSinglePlayer
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmrCountdown.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrCountdown.Start()
End Sub
Private Sub tmrCountdown_Tick(sender As Object, e As System.EventArgs) Handles tmrCountdown.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
tmrCountdown.Stop()
MessageBox.Show("Done")
End If
End Sub
End Class
这篇关于在VB.Net 2010中倒计时的计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!