0

我正在尝试将计时器从 30 分钟减少到 0 分钟,并随着时间的减少更新进度条

我的表单上有一个进度条控件并将其设置为 min val to '0' and max value to '60'andincremental step to '1'

我现在一直坚持..

这是我到目前为止所做的:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     ProgressBar1.Value = 60
End sub

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim dFrom As DateTime
        Dim dTo As DateTime
        Dim sDateFrom As String = DateTime.Now
        Dim sDateTo As String = lblLogOutTime.Text
        If RemainingTime.Text = "00:00:00" Then
            RemainingTime.Text = "Time's up!"
            ProgressBar1.Value = 0
            Timer2.Stop()
         ElseIf DateTime.TryParse(sDateFrom, dFrom) AndAlso DateTime.TryParse(sDateTo, dTo) Then
            Timer2.Start()
            ProgressBar1.Value -= 1
            ProgressBar1.Update()
            Dim TS As TimeSpan = dTo - dFrom
            Dim hour As Integer = TS.Hours
            Dim mins As Integer = TS.Minutes
            Dim secs As Integer = TS.Seconds
            Dim timeDiff As String = ((hour.ToString("00") & ":") + mins.ToString("00") & ":") + secs.ToString("00")
            RemainingTime.Text = timeDiff
 End If
4

1 回答 1

0

看起来你那里有很多额外的代码。例如将 DateTime 类型转换为字符串,然后再转换回 Date。

Private LogOutTime as Date          ' destination time

表单加载/重启子:

LogOutTime = DateTime.Now.AddMinutes(30)
' 1 min tick
Timer2.Interval = 60 * 1000         ' 1 min timer
ProgressBar1.Value = 30             ' 30 min countdown
ProgressBar1.Maximum = 31 
Timer2.Start

 Private Sub Timer2_Tick(...

    Dim Ts as TimeSpan = LogOutTime - DateTime.Now

    If Ts.Minutes = 0 Then           ' or TS,Ticks
        RemainingTime.Text = "Time's up!"
        ProgressBar1.Value = 0
        Timer2.Stop()
        Exit Sub
    End If

    ' better to convert Ts.Ticks to a 0-30 value
    ' so the timer interval can be different than 1 min
    ProgressBar1.Value -= 1
    RemainingTime.Text = New DateTime(Ts.Ticks).ToString("hh:mm:ss")
    Timer2.Start()

  End Sub

根据您的 .NET 版本,有几种方法可以格式化剩余时间。我不知道 Timer1 做什么,它可能会干扰。如果 Timer 间隔为 20 或 30 秒,您可以显示秒数,但您必须从 Timespan.Ticks 计算进度条值,而不是简单地递减。

于 2013-10-25T13:07:26.133 回答