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

时间:2013-05-18 19:43:08

标签: vb.net timer countdowntimer

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

在我的公共类的顶部,所以我能够在倒数计时器中输入指定的时间。这可能是令人难以置信的愚蠢,我无法弄清楚它是什么。

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

感谢您的帮助!

6 个答案:

答案 0 :(得分:4)

玩这个:

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

答案 1 :(得分:1)

采取倒数计时器的测试样本。进行所需的更改(以时间格式为准)。

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

答案 2 :(得分: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

答案 3 :(得分:0)

您原始代码中的错误是您正在错误地使用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

答案 4 :(得分:0)

更简单:

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

答案 5 :(得分: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