如何重复一个以信息框结束的定时器?

问题描述 投票:0回答:1

该程序的目的是提醒用户每30分钟休息一次。定时器还允许用户输入多久提醒一次。倒计时只在达到零时工作一次,然而,我想重复这个过程三次。骰子图像是占位符。对不起,任何新手的错误。

下面是代码。

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If lblHrs.Text = "0" And lblMin.Text = "0" And lblSec.Text = "0" Then
        dice1 = Int(Rnd() * 6 + 1)
        Select Case dice1

            Case "1"
                DiceRoll1.Image = My.Resources._1
            Case "2"
                DiceRoll1.Image = My.Resources._2
            Case "3"
                DiceRoll1.Image = My.Resources._3
            Case "4"
                DiceRoll1.Image = My.Resources._4
            Case "5"
                DiceRoll1.Image = My.Resources._5
            Case "6"
                DiceRoll1.Image = My.Resources._6

        End Select
        Timer1.Enabled = False

    End If

    Dim seconds As Double
    Dim minutes As Double
    Dim hours As Double

    Double.TryParse(lblSec.Text, seconds)
    Double.TryParse(lblMin.Text, minutes)
    Double.TryParse(lblHrs.Text, hours)

    If seconds = 0 And minutes <> 0 Then
        lblSec.Text = 59
        lblMin.Text = minutes - 1
    ElseIf seconds > 0 Then
        lblSec.Text = seconds - 1
    End If
    If minutes = 0 And hours <> 0 Then
        lblMin.Text = 59
        lblHrs.Text = hours - 1
    End If

    Dim Msg, Style, Title
    Msg = "Time to rest and exercise."    ' Define message.
    Style = vbOKOnly + vbInformation    ' Define buttons.
    Title = "Rest reminder"    ' Define title.

    ' Display message.
    MsgBox(Msg, Style, Title)

End Sub
vb.net loops timer messagebox
1个回答
0
投票

这不是对你问题的完整回答,但我想发布一大块代码,所以评论不是一个好的选择。如果你想实现一个倒计时,这个类会帮助你。

Public Class CountdownStopwatch
    Inherits Stopwatch

    Public Property Period As TimeSpan

    Public ReadOnly Property Remaining As TimeSpan
        Get
            Dim timeRemaining = Period - Elapsed

            Return If(timeRemaining > TimeSpan.Zero, timeRemaining, TimeSpan.Zero)
        End Get
    End Property

    Public ReadOnly Property IsExpired As Boolean
        Get
            Return Elapsed >= Period
        End Get
    End Property

End Class

它基本上是一个 Stopwatch 具有额外的倒计时功能。例如,如果你想做10分钟的事情,那么你可以这样做。

myCountdownStopwatch.Period = TimeSpan.FromMinutes(10)
myCountDownStopwatch.Restart()

在... Tick 您的 Timer可以像这样显示剩余时间,并在时间到期时提示用户。

timeReaminingLabel.Text = myCountdownStopwatch.Remaining.ToString("m\:ss")

If myCountdownStopwatch.IsExpired Then
    myTimer.Stop()
    myCountdownStopwatch.Stop()
    myCountdownStopwatch.Reset()

    'Prompt user here.
End If
© www.soinside.com 2019 - 2024. All rights reserved.