2013-10-11 41 views
0

我想創建一個計時器,從指定的時間倒計時。編程vb.net,第二種形式的文本框不會更新

用戶輸入時間並單擊按鈕。
按鈕點擊打開第二個窗體,其中有一個計時器。
每當計時器滴答時間,時間就會減少,剩下的時間會顯示在窗體2上的文本框textbox.text = timeLeft)中。

但是,文本框將永遠不會實際更新。它仍然是空白,這賦予了新的價值的.text屬性將實際工作的唯一情況是如果我提出一個事件(例如點擊一個按鈕,將改變文本框.text屬性)

*這裏是Timer類

Public Class CountdownTimer 

Private timeAtStart As Integer 
Private timeLeft As Integer 

Public Sub StartTimer(ByVal time As Integer) 
    timeAtStart = time 
    timeLeft = timeAtStart 
    Timer1.Enabled = True 
End Sub 


Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick 

    If timeLeft > 0 Then 
     timeLeft = timeLeft - 1 
     txtTimeLeft.Text = timeLeft.ToString 

    Else 
     Timer1.Stop() 
     txtTimeRemaining.Text = "Time!" 
     txtTimeRemaining.ForeColor = Color.Red 
    End If 

End Sub 

End Class 
  • 這裏的代碼我怎麼稱呼它:

    Dim timer As New CountdownTimer 
    timer.Show() 
    CountdownTimer.StartTimer(CInt(txtSetTime.Text)) 
    
+3

放置代碼。 –

+0

沒有看到代碼,我的猜測是你需要更新Tick事件的文本框 – TEK

+0

在tick事件中設置一個斷點...我敢打賭,它從來沒有被調用...是'CountdownTimer'類還是表單? Timer1在哪裏?它在表格上嗎?無論這些東西是什麼,你最後的部分(我怎麼稱呼它)是錯誤的。 – Plutonix

回答

2

你的代碼調用(表單)類而不是實例,我不能看到Timer1適合引用一個獨立的可重用類。這裏是實現一個倒計時類,將與其他形式合作的一種方式....

Friend Class CountdownTimer 

    Private timeAtStart As Integer 
    Private timeLeft As Integer 
    Private WithEvents Timer1 As New Timer 
    Private txtTimeLeft as TextBox 

    Public Sub New(TargetTB as TextBox) 
     txtTimeLeft= TargetTB 
    End Sub 


    Public Sub StartTimer(ByVal time As Integer, timeLength as Integer) 
     timeAtStart = time 
     timeLeft = timeLength 

     Timer1.Enabled = True 
    End Sub 


    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs)_ 
      Handles Timer1.Tick 

     ' just dislaying time left 
     If timeLeft > 0 Then 
      timeLeft = timeLeft - 1 
      txtTimeLeft.Text = timeLeft.ToString 

     Else 
      Timer1.Stop() 
      txtTimeLeft.Text = "Time!" 
      txtTimeLeft.ForeColor = Color.Red 
     End If 

    End Sub 

End Class 

如何使用它:

Dim CountDn As New CountdownTimer(frm.TextBoxToUse) 

' use the INSTANCE name not the class name!!!! 
'CountdownTimer.StartTimer(CInt(txtSetTime.Text)) 
CountDn.StartTimer(CInt(txtSetTime.Text)) 
0

如果顯示的結果計時器完成後,我想你應該使用

Application.DoEvents() 

方法立即看到更新。它實際上適用於Windows窗體。你有什麼嘗試,所以我可以進一步幫助

+0

我試圖做到這一點,我也試過me.refresh,txtTimeRemaining.refresh和me.show。這些都沒有奏效。 –

+0

不推薦在VB.NET中使用DoEvents,所以我不會推薦它。如果這是問題,最好刷新使用'.Refresh'更改的對象。 –

0

你一定要明白,當你掰着指頭要設置與完成時不同的文本框,對嗎?

txtTimeLeft.Text 

VS

txtTimeRemaining.Text 

注:計時器的同一個線程中UI上運行,所以如果你的計算機(或程序)變得繁忙,定時器將不以精確的時間間隔打勾。如果您擔心計時器中的小差異,則應該比較每次計時事件期間計算機時間的差異,以確定已經過了多少時間。

Dim TS = TimeSpan = Now.Subtract(StartingTime) 
0

嘗試每次更新後刷新文本框:

所以經過

txtTimeLeft.Text = timeLeft.ToString 

添加

txtTimeLeft.Refresh 
0

這是你的問題:

Dim timer As New CountdownTimer 
timer.Show() 
CountdownTimer.StartTimer(CInt(txtSetTime.Text)) 

你實例化一個名爲timer新的對象,但隨後開始計時的CountdownTimer對象

您需要更改您的代碼這:您曾使用請

Dim timer As New CountdownTimer 
timer.Show() 
timer.StartTimer(CInt(txtSetTime.Text)) 
+0

好吧,原始問題的其他部分(我認爲)是一個(或多個)TB處於不同的形式,所以在tick事件中,需要添加。 – Plutonix

相關問題