利用Timer做簡單的倒數計時

摘要:利用Timer做簡單的倒數計時

瀏覽文章時看到下面這篇如何編寫 Visual Basic Timer ,是詢問如何做倒數顯示的功能,之前還沒有玩過這樣的功能,這邊簡單測試了一下,還滿好玩的;下面是測試的程式碼,可以參考看看

  1. Dim StartTime As DateTime  
  2.   
  3.     Private Sub Button1_Click(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Button1.Click  
  4.         StartTime = Now  
  5.         Timer1.Enabled = True  
  6.     End Sub  
  7.   
  8.     Private Sub Timer1_Tick(ByVal sender As System.ObjectByVal e As System.EventArgs) Handles Timer1.Tick  
  9.         TextBox1.Text = (Now - StartTime).TotalSeconds  
  10.         TextBox2.Text = (DateTimePicker1.Value - Now).TotalSeconds  
  11.     End Sub  

Dim StartTime As DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        StartTime = Now
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Text = (Now - StartTime).TotalSeconds
        TextBox2.Text = (DateTimePicker1.Value - Now).TotalSeconds
    End Sub


範例程式碼也可以在這邊下載