首页 > 解决方案 > 如何重复以消息框结尾的计时器?

问题描述

该程序的目的是提醒用户每 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.netloopstimermessagebox

解决方案


这不是您问题的完整答案,但我想发布一段代码,所以评论不是一个好的选择。如果您尝试实现倒计时,该课程将为您提供帮助:

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()

如果TickTimer,您可以显示剩余时间并在时间到期时提示用户,如下所示:

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

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

    'Prompt user here.
End If

推荐阅读