简体   繁体   English

Vb.net倒计时

[英]Vb.net countdown

I'm trying to make a countdown timer in my app. 我正在尝试在我的应用中创建一个倒数计时器。 I already know the countdown time which is 4 minutes. 我已经知道倒计时时间是4分钟。 I have a Timer which ticks every seconds. 我有一个Timer ,每秒钟滴答作响。 Now from that how can I update my TextBox so that it shows the time remaining in the format HHMMSS ? 现在,从中如何更新TextBox ,使其以HHMMSS格式显示剩余时间?

EDIT: the problem I'm having is calculating the remaining time. 编辑:我遇到的问题是计算剩余时间。 What should I use? 我应该使用什么? Timestamp? 时间戳?

Before you start counting down, note the current time using something like 在开始递减计数之前,请使用类似以下记录当前时间

Dim endTime as DateTime = DateTime.Now.Add(TimeSpan.FromMinutes(4))

Then when your tick events occur, you can figure out how much time is left using 然后,当您的滴答事件发生时,您可以计算出剩余时间

Dim timeLeft as TimeSpan = endTime.Subtract(DateTime.Now)

You can format timeLeft as needed, possibly using .ToString("hh:mm:ss") mentioned by Jared 您可以根据需要设置timeLeft的格式,可能使用Jared提到的.ToString(“ hh:mm:ss”)

You can read the docs on TimeSpan s and DateTime s for more info on how to use them. 您可以阅读有关TimeSpanDateTime的文档,以获取有关如何使用它们的更多信息。

The best way to keep track of the time is to log the start by using DateTime.Now. 跟踪时间的最佳方法是使用DateTime.Now记录开始时间。 Then you can see the difference at any time by subtracting the saved value. 然后,您可以通过减去已保存的值随时查看差异。

Dim diff = DateTime.Now - savedTime
Dim remaining = TimeSpan.FromMinutes(4) - diff

To get the format you want, just pass it to the .ToString function as a string 要获取所需的格式,只需将其作为字符串传递给.ToString函数

Dim readable = remaining.ToString("hh:mm:ss")

Add an event handler for the timer and have it calculate the remaining time, format a string and update the textbox. 为计时器添加事件处理程序,并使其计算剩余时间,设置字符串格式并更新文本框。

To track time and calculate the difference you can use Environment.TickCount - it's the simplest variant. 要跟踪时间并计算差异,可以使用Environment.TickCount-这是最简单的变体。 Save the timestamp at the moment you start the countdown and get it each time the event handler is called. 保存倒计时开始时的时间戳,并在每次事件处理程序被调用时获取时间戳。

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Interval = 500
    Timer1.Start()
End Sub
Dim stpw As New Stopwatch
Dim CountDownFrom As Integer = 4 * 60 'as seconds
Dim CountDown As New TimeSpan
Private Sub StartCountDown()
    CountDown = TimeSpan.FromSeconds(CountDownFrom)
    stpw.Stop() : stpw.Reset() : stpw.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If Not stpw.IsRunning Then Exit Sub
    Dim TimeRemaining As TimeSpan = CountDown - stpw.Elapsed
    Label1.Text = TimeRemaining.Hours.ToString.PadLeft(2, "0"c) & ":" & _
                    TimeRemaining.Minutes.ToString.PadLeft(2, "0"c) & ":" & _
                    TimeRemaining.Seconds.ToString.PadLeft(2, "0"c)
    If TimeRemaining.TotalMilliseconds <= 0 Then stpw.Stop()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    StartCountDown()
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM