在 VB.Net 2010 中倒计时的计时器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16628296/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:54:34  来源:igfitidea点击:

Timer to count down in VB.Net 2010?

vb.nettimercountdowntimer

提问by Holy Dribble

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:SSand then stop when the time reaches 00:00.

我正在尝试使用计时器从我选择的指定时间开始倒计时,使用格式MM:SS将时间分为分钟和秒,然后在时间到达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;

例如,从 120 秒开始倒计时;

02:00> 02:59> 02:58> 02:57> 02:56> 02:55

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;

然后在同样的测试下继续倒计时90秒;

02:30> 01:29> 01:28> 01:27> 01:26> 01:25

02:30> 01:29> 01:28> 01:27> 01:26> 01:25

When the countdown reaches 00or 30seconds, it incorrectly displays the minutes left and can't understand or figure out how to fix it.

当倒计时到达0030秒时,它会错误地显示剩余的分钟数并且无法理解或弄清楚如何解决它。

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!

感谢您的帮助!

回答by Idle_Mind

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

回答by Emmanouil Chountasis

Take a tested sample of countdown timer. Make the changes you need(ex the format of the time).

取倒数计时器的测试样本。进行您需要的更改(例如时间格式)。

Sub New()

子新建()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        SetTime = 70

        AddHandler dtTimer.Tick, AddressOf dtTimer_Tick
        dtTimer.Interval = New TimeSpan(0, 0, 1)
        dtTimer.Start()
    End Sub

    Private Property SetTime As Integer

    Private Sub dtTimer_Tick(sender As Object, e As EventArgs)

        Dim iMinutes As Integer
        Dim iSeconds As Integer

        If SetTime = 0 Then
            dtTimer.Stop()
            txtTime.Text = "0:0"
            Exit Sub
        End If

        SetTime -= 1
        iMinutes = Math.Floor(SetTime / 60)
        iSeconds = SetTime Mod 60

        txtTime.Text = iMinutes & ":" & iSeconds
    End Sub

回答by dbasnett

Try this

尝试这个

'the amount of time to countdown from
Dim countDownFrom As New TimeSpan(0, 0, 10) 'ten seconds
'a Stopwatch to track how long running
Dim stpw As New Stopwatch

Private Sub Button1_Click(sender As Object, _
                          e As EventArgs) Handles Button1.Click
    Timer1.Interval = 250 'how often to update display
    Timer1.Start() 'start the display updater
    stpw.Reset() 'restart the stopwatch
    stpw.Start()
    'or depending on version of .Net
    'stpw.Restart
End Sub

Private Sub Timer1_Tick(sender As Object, _
                        e As EventArgs) Handles Timer1.Tick
    If stpw.Elapsed <= countDownFrom Then
        Dim toGo As TimeSpan = countDownFrom - stpw.Elapsed
        lblTime.Text = String.Format("{0:00}:{1:00}:{2:00}", toGo.Hours, toGo.Minutes, toGo.Seconds)
    Else
        Timer1.Stop()
        stpw.Stop()
    End If
End Sub

回答by user2517766

your mistake in your original code is that you are using the MOD operater incorrectly.

您在原始代码中的错误是您错误地使用了 MOD 操作员。

   'Minutes
    Min = ((Time - Sec) / 60) Mod 60

    'Seconds
    Sec = Time Mod 60

At 2:00 you see 2:00 because:

在 2:00,您会看到 2:00,因为:

Min = ((120-00) / 60 ) MOD 60 = 2 MOD 60 = 2

Sec = 120 MOD 60 = 0

At 1:59 you see 2:59 because

在 1:59 你看到 2:59 因为

Min = (119 / 60) MOD 60 = 1.98 MOD 60 = 1.98 = 2

Sec = 119 MOD 60 = 59

After 31 seconds your minutes changes from 2 to 1 because

31 秒后,您的分钟数从 2 变为 1,因为

Min = (89 / 60) MOD 60 = 1.48 MOD 60 = 1.48 = 1

回答by Julien

More simply:

更简单:

Format((Math.Floor(lSeconds / 60)), "00") & ":" & Format((lSeconds Mod 60), "00")