Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,097 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,861 people online right now. Registration is fast and FREE... Join Now!




Countdown Timer

 
Reply to this topicStart new topic

Countdown Timer

Zander
2 Jun, 2008 - 06:15 PM
Post #1

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 6

Hi everyone,
Im having some problems with the timer component, i want label10 to countdown with the timer component to 0
the user inputs the time in Miliseconds in textbox2

so i want to let the user see the time being counted down.
Any help is much appriciated smile.gif
-Zander

Edit* Ooops! i think i may have put this in the wrong section, im Using VB.NET
My Apologies

CODE
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox2.Text = ("") Or TextBox1.Text = ("") Then
            MessageBox.Show("Syntax Error, Fill in all fields")
        Else
            Timer1.Interval = TextBox2.Text 'Timer inputted by user
            Timer1.Enabled = True
            Label3.Text = TextBox1.Text
            Label10.Text = Timer1.Interval. / 1000 'Time in seconds
            MessageBox.Show("Timer Added")
        End If

CODE
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim Reminder As String = "Timer: " + TextBox1.Text + " Has Expired"
        MsgBox(Reminder, MsgBoxStyle.Information And MsgBoxStyle.OkOnly, "Your Reminder!")
        Timer1.Enabled = False
    End Sub


This post has been edited by Zander: 2 Jun, 2008 - 06:17 PM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Countdown Timer
2 Jun, 2008 - 06:18 PM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,163



Thanked: 78 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
** Moved to VB.NET **
User is online!Profile CardPM
+Quote Post

RodgerB
RE: Countdown Timer
2 Jun, 2008 - 11:21 PM
Post #3

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
If it were me, this is the code I'd be using.

vb
Dim stopWatch As New Stopwatch()
Dim totalInterval As Integer = 0

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' How often the timer will check.
Timer1.Interval = 100

' Check if it is running.
If stopWatch.IsRunning Then
stopWatch.Stop()
stopWatch.Reset()
Timer1.Stop()
End If

' Start the stopwatch / timer.
stopWatch.Start()
Timer1.Start()

' Set the interval.
Try
totalInterval = TextBox2.Text
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
' Show the time remaining to the user.
Label10.Text = Convert.ToInt32(totalInterval - stopWatch.Elapsed.TotalMilliseconds).ToString()

' Check to ensure the timer stops.
If stopWatch.Elapsed.TotalMilliseconds >= totalInterval Then
stopWatch.Stop()
stopWatch.Reset()
Timer1.Stop()

Label10.Text = "0"
End If
End Sub


I used the StopWatch class, as you can't be sure a Timer is going to fire on every second, with interrupts such as System Calls, Events etc. The StopWatch on the other hand, is pretty good with stuff like this.

Hope that helps. smile.gif
User is offlineProfile CardPM
+Quote Post

Zander
RE: Countdown Timer
3 Jun, 2008 - 02:13 PM
Post #4

New D.I.C Head
*

Joined: 2 Jun, 2008
Posts: 6

QUOTE(RodgerB @ 3 Jun, 2008 - 12:21 AM) *

If it were me, this is the code I'd be using.

<Code>

I used the StopWatch class, as you can't be sure a Timer is going to fire on every second, with interrupts such as System Calls, Events etc. The StopWatch on the other hand, is pretty good with stuff like this.

Hope that helps. smile.gif


That work Perfectly, Thank you very much!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:20AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month