[VB.net]倒數計時器
嘗試以VB.net學習撰寫一支倒數計時程式,簡易成果如上圖,code如下列所示。
Public Class Form1
Private Sub StBt_Click(sender As Object, e As EventArgs) Handles StBt.Click
PrBar.Value = 0
TB2.Text = ""
Timer1.Start()
End Sub
Private Sub PaBt_Click(sender As Object, e As EventArgs) Handles PaBt.Click
Timer1.Stop()
End Sub
Private Sub ClBt_Click(sender As Object, e As EventArgs) Handles ClBt.Click
Timer1.Stop()
PrBar.Value = 0
TB2.Text = ""
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim i As Integer
Dim ratio As Single
Timer1.Interval = 1000 ' timer單位為1ms,1000指的是set timer1以秒計算
PrBar.Increment(PrBar.Maximum / TB1.Text) 'progress bar以輸入TB1值呈現比例顯示
ratio = PrBar.Value / PrBar.Maximum '顯示進度比例於日誌進度中
TB2.AppendText(DateTime.Now + " 進度 " + ratio.ToString("p2") + Environment.NewLine) '日誌記錄
If PrBar.Value = PrBar.Maximum Then '如果progress bar已完成,則Timer停止
Timer1.Stop()
MsgBox("倒數完畢")
End If
End Sub
Private Sub TB1_TextChanged(sender As Object, e As EventArgs) Handles TB1.TextChanged
Timer1.Stop()
PrBar.Value = 0
TB2.Text = ""
End Sub
End Class