שתף באמצעות


Countdown timer in vb? Any help?

Question

Monday, December 31, 2012 1:21 PM

Hi,

I need help on making a countdown timer in visual basic.

I have already added a timer and a label.

But when I start the form, it says "05:00" in the label and then it says "00:300000"

Can anyone help?

Here is my code:

Public Class Form3
    Dim timeLeft As Integer

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        If timeLeft > 0 Then
            timeLeft -= 1
            ToolStripLabel3.Text = timeLeft
        Else
            Timer1.Stop()
            ToolStripLabel3.Text = "Time's up!"
            MsgBox("You didn't finish in time.", "Sorry")
        End If
    End Sub

    Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
        ToolStripLabel3.Text = "00:" & timeLeft
    End Sub
End Class

Regards,

Ali Hamza Mohammed

All replies (5)

Monday, December 31, 2012 2:03 PM ✅Answered

From link http://codingangel.com/countdown-timer-in-vb-net/

Public Class Form1

    Dim m_StopTime As Date

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim duration As TimeSpan = TimeSpan.Parse(TextBox1.Text)
        m_StopTime = Now.Add(duration)
        Timer1.Enabled = True
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim remaining As TimeSpan = m_StopTime.Subtract(Now)
        remaining = New TimeSpan(remaining.Hours, _
        remaining.Minutes, remaining.Seconds)
        If remaining.TotalSeconds < 0 Then remaining = _
        TimeSpan.Zero
        Label1.Text = remaining.ToString
        If remaining.TotalSeconds <= 0 Then
            Me.WindowState = FormWindowState.Maximized
            Me.TopMost = True
            Timer1.Enabled = False
        End If
    End Sub

End Class

You've taught me everything I know but not everything you know.


Monday, December 31, 2012 2:39 PM ✅Answered | 1 vote

try this:

http://code.msdn.microsoft.com/countDownTimer-Control-2da92ac3

thanks for any help


Monday, December 31, 2012 4:50 PM ✅Answered

Hello Ali Hamza Mohammed,

Hi,

I need help on making a countdown timer in visual basic.

I have already added a timer and a label.

But when I start the form, it says "05:00" in the label and then it says "00:300000"

Can anyone help?

Here is my code:

Public Class Form3    Dim timeLeft As Integer    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick        If timeLeft > 0 Then            timeLeft -= 1            ToolStripLabel3.Text = timeLeft        Else            Timer1.Stop()            ToolStripLabel3.Text = "Time's up!"            MsgBox("You didn't finish in time.", "Sorry")        End If    End Sub    Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter        ToolStripLabel3.Text = "00:" & timeLeft    End SubEnd Class

Regards,

Ali Hamza Mohammed

another sample of CountDown, http://www.codeproject.com/Articles/26872/Simple-countdown-chronometer , is C# , but convertable in vb net with this tool http://www.developerfusion.com/tools/convert/csharp-to-vb/

Regards.


Tuesday, January 1, 2013 3:20 AM ✅Answered

 Why not simply this ??

Public Class Form1

    Dim t As TimeSpan

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        t = New TimeSpan(0, 5, 0)
        Timer1.Interval = 1000
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        t -= New TimeSpan(0, 0, 1)
        Label1.Text = t.ToString.Substring(3)
        If t = New TimeSpan(0, 0, 0) Then
            Timer1.Stop()
            MsgBox("Time out")
        End If

    End Sub

End Class

Monday, December 31, 2012 2:47 PM | 1 vote

Monkeyboy,

Add  a Label1.show into your code, then the timer really runs.

(And in my idea remove that topmost that creates an awful irritating situation that you cannot hide the timer).

Success
Cor