vb.net - 如何重复以消息框结尾的计时器?
问题描述
该程序的目的是提醒用户每 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
解决方案
这不是您问题的完整答案,但我想发布一段代码,所以评论不是一个好的选择。如果您尝试实现倒计时,该课程将为您提供帮助:
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
推荐阅读
- php - Symfony 表单集合:如何防止在 EntityManager->flush() 上重复插入?
- laravel - Laravel 关系,多对多还是多对多?
- angular - Angular AuthGuard 和登录持久性:即使已登录,路由器也会自动重定向到非真实路由
- ios - How to observe Text in the SwiftUI or pass String to the ViewModel in other way?
- c# - c# RestClient 在发送以 XML 作为参数的 Post 请求时出错
- database - 如何参数化 Lua 脚本以通过执行查询的表值
- reactjs - React 如何使用 setTimeout 处理递归 API 调用
- python-3.x - 重新替换问题 python3
- python - Snowflake SqlAlchemy - 检查 api
- bash - 使用 cron 运行带有 Xvfb 显示的 sh?