2

我正在尝试使用计时器从我选择的指定时间开始倒计时,时间使用格式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

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

这是我的计数计时器代码;

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

这是我格式化时间的函数代码;

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

这是我的表单加载代码;

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

我已经设置了;

    Dim SetTime As Integer

在我的公共课的顶部,所以我可以在倒数计时器中输入指定的时间。这可能是非常愚蠢的事情,我无法弄清楚它是什么。

非常感谢任何帮助,请记住,我是编程初学者,很容易与大量代码混淆。(我几乎无法理解该功能。)

感谢您的帮助!

4

6 回答 6

5

玩这个:

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
于 2013-05-19T07:01:40.957 回答
1

尝试这个

'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
于 2013-05-19T15:12:24.163 回答
1

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

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

    'Seconds
    Sec = Time Mod 60

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

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

Sec = 120 MOD 60 = 0

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

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

Sec = 119 MOD 60 = 59

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

Min = (89 / 60) MOD 60 = 1.48 MOD 60 = 1.48 = 1
于 2013-07-30T07:31:41.737 回答
1

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

子新()

        ' 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
于 2013-05-18T20:41:09.893 回答
0
Public SetTime As Integer = 0
Public Min As Integer = 0
Public Sec As Integer = 0
Public Decimaal As Decimal = 0

Public Function FormatTime(ByVal Time As Integer) As String   
  'Minutes
  Min = Fix(SetTime / 60000)
  'Decimaal
  Decimaal = (SetTime / 60000) - Min
  'Seconden
  Sec = Fix(Decimaal * 60)

  Return Format(Min, "00") & ":" & Format(Sec, "00")

End Function

Private Sub tmrCountdown_Tick(sender As Object, e As EventArgs) Handles tmrCountdown.Tick
        SetTime = SetTime - 1000
        lblCountdown.Text = FormatTime(SetTime)
        tmrCountdown.Enabled = True

        If SetTime = 0 Then
            tmrCountdown.Enabled = False
        End If
End Sub
于 2021-04-01T05:17:10.343 回答
0

更简单地说:

Format((Math.Floor(lSeconds / 60)), "00") & ":" & Format((lSeconds Mod 60), "00")
于 2018-12-26T12:43:04.767 回答