2013-03-18 205 views
0

我越來越想顯示操作的時間remainig顯示剩餘時間時間跨度

的問題是隻能用剩下的計時器,我得到一個負值的問題...

這是一個例子10000毫秒(10秒)一個操作:

enter image description here

所以無論如何,如果我刪除「 - 」從時間跨度小時和分鐘劇照incorrects值字符...

Dim time_out as integer = 60000 ' 'Milisegundos 

    Dim StartTime As DateTime ' Tiempo inicio 
    Dim EndTime As DateTime ' Tiempo final 

    Dim ElapsedTime As TimeSpan ' Tiempo transcurrido 
    Dim RemainingTime As TimeSpan ' Tiempo restante 


    ' Elapsed Time 
#Region " Elapsed Time Function " 

    Public Function Print_Elapsed_Time() 
     If StartTime.ToString = "01/01/0001 0:00:00" Then 
      StartTime = Now 
      StartTime = StartTime.AddSeconds(-1) 
     End If 
     ElapsedTime = Now().Subtract(StartTime) 
     Return String.Format("{0:00}:{1:00}:{2:00}", CInt(Math.Floor(ElapsedTime.TotalHours)) Mod 60, CInt(Math.Floor(ElapsedTime.TotalMinutes)) Mod 60, CInt(Math.Floor(ElapsedTime.TotalSeconds)) Mod 60) 
    End Function 
#End Region 

#Region " Remaining Time Function " 

    Public Function Print_Remaining_Time() 
     If EndTime.ToString = "01/01/0001 0:00:00" Then 
      EndTime = Now 
      EndTime = EndTime.AddMilliseconds(Time_Out - 1000) 
     End If 
     RemainingTime = Now().Subtract(EndTime) 
     Return String.Format("{0:00}:{1:00}:{2:00}", CInt(Math.Floor(RemainingTime.TotalHours)) Mod 60, CInt(Math.Floor(RemainingTime.TotalMinutes)) Mod 60, CInt(Math.Floor(RemainingTime.TotalSeconds)) Mod 60).Replace("-", "") 
    End Function 

#End Region 

回答

1

我認爲結束時間你想:

RemainingTime = EndTime.Subtract(Now) 

否則,我不肯定你是如何初始化這些的,但是我做了這個改變:

Dim StartTime As DateTime = DateTime.MinValue ' Tiempo inicio 
Dim EndTime As DateTime = DateTime.MinValue ' Tiempo final 

還有:

If StartTime = DateTime.MinValue Then ' ... etc ' 

但也許一個標誌或什麼是一個更好的方式來發信號重置。

+0

Thankyou簡單的解決方案! – ElektroStudios 2013-03-18 18:47:10

2

下面是一個使用定時器和兩個標籤從10秒倒計時的例子

'example - countdown from 10 secs 
Dim countdown As New TimeSpan(0, 0, 10) 
Dim stpw As New Stopwatch 

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    If stpw.IsRunning Then 
     stpw.Stop() 
     Timer1.Stop() 
    Else 
     stpw.Stop() 
     stpw.Reset() 
     stpw.Start() 
     Timer1.Interval = 100 
     Timer1.Start() 
    End If 
End Sub 

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
    'label1 - elapsed time 
    'label2 - time remaining 
    If stpw.Elapsed <= countdown Then 
     Label1.Text = stpw.Elapsed.ToString 
     Label2.Text = (countdown - stpw.Elapsed).ToString 
    Else 
     stpw.Stop() 
     Label1.Text = countdown.ToString 
     Label2.Text = "00:00:00" 
    End If 
End Sub 
+0

謝謝你的解決方案! – ElektroStudios 2013-03-18 18:47:57