count up/down with Timers

mikewitney

New member
Joined
Jan 22, 2008
Messages
3
Programming Experience
Beginner
i need to have 3 timers running in one form

one wil count down from a time inputed by the user ie 4hrs 30 mins

and the other two will just count up and can be paused at anytime

can anyone point me in the right direction
i am using visual stuidio 2008
and i am still learning i am using a book called visual basic.net and visual basic.net for dummies

this is an application for me as i am an hgv driver and i am looking at a way to help me keep my driving times in check

this is being used on my pda windows mobile 2005
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
timers (from the toolbox) have an interval property which is the number of milliseconds until the timer_tick event occurs. you could start a timer with an interval of 4.5 hours, or you could use a timespan
have a look at this thread for how to use timespans
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,377
Location
Norway
Programming Experience
10+
you could start a timer with an interval of 4.5 hours, or you could use a timespan
If you wanted a timer to count down from any given time, wouldn't it be pretty stupid to set the interval to this time? Nothing would happen during this interval. How could that enable the code to count down?
 

.paul.

Well-known member
Joined
May 22, 2007
Messages
212
Programming Experience
1-3
If you wanted a timer to count down from any given time, wouldn't it be pretty stupid to set the interval to this time? Nothing would happen during this interval. How could that enable the code to count down?
i meant if you just want to be notified after 4.5 hours.
alternatively i recommended the timespan method
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
Timers are not used to time things. A Timer is like an alarm clock. It simply notifies you when an amount of time has elapsed. If you want to actually measure how long something takes then you can use a Stopwatch.

If you want to display a timed countdown then would use a Timer but not to actually measure the time. You would simply create a Date object that represented when you wanted the countdown to end. You could then use a Timer to display the time remaining at an interval, which would likely be 1 second.
Code:
Private endTime As Date

Private Sub StartCountdown(ByVal time As TimeSpan)
    Me.endTime = Date.Now + time
    Me.Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As Object, _
                        ByVal e As EventArgs) Handles Timer1.Tick
    Dim timeRemaining As TimeSpan = Me.endTime - Date.Now

    If timeRemaining > TimeSpan.Zero Then
        Me.Label1.Text = String.Format("{0}:{1:00}:{2:00}", _
                                       Math.Floor(timeRemaining.TotalHours), _
                                       timeRemaining.Minutes, _
                                       timeRemaining.Seconds)
    Else
        Me.Timer1.Stop()
        Me.Label1.Text = "0:00:00"
        MessageBox.Show("Time's up!")
    End If
End Sub
 

mikewitney

New member
Joined
Jan 22, 2008
Messages
3
Programming Experience
Beginner
with
Me.Timer1.Start()
and
Me.Timer1.Stop()

i get the following bear in mind im a newbie sorry if its obvious

Error 2 'Stop' is not a member of 'System.Windows.Forms.Timer'
Error 2 'Start' is not a member of 'System.Windows.Forms.Timer'
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
with
Me.Timer1.Start()
and
Me.Timer1.Stop()

i get the following bear in mind im a newbie sorry if its obvious

Error 2 'Stop' is not a member of 'System.Windows.Forms.Timer'
Error 2 'Start' is not a member of 'System.Windows.Forms.Timer'
This page is for the 3.5 Framework (Which I also know is for the v2.0 and v1.1 Frameworks too)
http://msdn2.microsoft.com/en-us/library/system.windows.forms.timer_members.aspx

This page shows the Windows.Forms.Timer has: Start() and Stop() methods
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
That is a very strange error because it seems very clear what it is saying but, as JB has shown, those methods very definitely are members of the Timer class. Could you maybe post a screen shot of your IDE showing your code and those error messages? If we can't figure out why that's happening then you could try setting the Enabled property instead, but I can't see why that property would work and those methods wouldn't.
 

UncleRonin

Well-known member
Joined
Feb 28, 2006
Messages
230
Location
South Africa
Programming Experience
5-10
*scratches* I've never once in my life used the Start() and Stop() methods. I'm old school and have always just set Timer.Enabled = true or Timer.Enabled = false (which is pretty much the same thing) I guess this is because I haven't ever actually used the new stuff... one day...

Since it's Windows Mobile it will be using the Compact Framework and that doesn't support Start() and Stop(). Just try setting Timer.Enabled and that should do the trick.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,251
Location
Sydney, Australia
Programming Experience
10+
Aha! I'd forgotten about the Mobile thing. I guess that's why there's no Start and Stop methods. It looks like someone has moved this thread to a more appropriate forum. If you post in a Mobile forum then people won't forget it's a Mobile question.
 
Top Bottom