倒數時間的作法


 Dim timer1 As New Timer
    Dim lblCD As New Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With Me
            .Tag = Now.AddSeconds(10) 'final countdown
            .Size = New Size(250, 60)
            .WindowState = FormWindowState.Normal
            .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
            .ControlBox = False
            .Controls.Add(lblCD)
        End With
        With lblCD
            .Font = New Font("arial", 10)
            .Size = New Size(Me.Width, Me.Height)
            .Location = New Point(20, 5)
        End With
        timer1.Interval = 100
        AddHandler timer1.Tick, AddressOf timer1_Tick
        timer1.Start()
    End Sub
    Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
        If CountDown(Me.Tag, Date.Now) <> Nothing Then
            lblCD.Text = CountDown(Me.Tag, Date.Now)
        Else
            lblCD.Text = "Time's up."
            timer1.Stop()
        End If
    End Sub
    Function CountDown(ByVal tmpBegin As Date, ByVal tmpCurrent As Date) As String
        'About TimeSpan please Refer follow link
        'http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-TW&k=k(SYSTEM.TIMESPAN)&rd=true
        Dim elapsedTicks As Long = tmpBegin.Ticks - tmpCurrent.Ticks
        Dim elapsedSpan As New TimeSpan(elapsedTicks)
        If elapsedSpan.TotalSeconds > 0 Then
            Return String.Format("CountDown: {0}天{1}時{2}分{3}秒{4}", elapsedSpan.Days, elapsedSpan.Hours, _
                                 elapsedSpan.Minutes, elapsedSpan.Seconds, Mid(elapsedSpan.Milliseconds, 1, 2))
        Else
            Return Nothing
        End If
    End Function
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Do
            Me.Text = String.Format("{0:yyyy-MM-dd hh:mm:ss.ff}", Now)
            Application.DoEvents()
            Threading.Thread.Sleep(100)
        Loop
    End Sub

沒有留言:

張貼留言