Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a countdown timer on my form that counts down from 20:00 minutes.. but I would also like to add another timer that counts the time elapsed (the time the form has been open, ill call that playing time)

 

I want it so it updates the label each second with how long the form has been open.

 

so 00:01, then 00:02, etc..

 

Heres the code i used for the countdown timer.. maybe someone knows how i can reverse it to count up instead.

   Private myStartTime As DateTime

       'Sets timer to countdown when enabled
       Dim myTimeElapsed As TimeSpan = DateTime.Now.Subtract(myStartTime)
       Dim myTimeLeft As New TimeSpan(0, 20, 1)
       myTimeLeft = myTimeLeft.Subtract(myTimeElapsed)
       lblTime.Text = myTimeLeft.Minutes.ToString("00") & ":" & myTimeLeft.Seconds.ToString("00")

Posted

ok, heres what i tried, and im getting one error:

   Private myPlayingTime As DateTime

   Private Sub TimePlaying_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimePlaying.Tick

       Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)
       Dim myTotalTime As New TimeSpan(0, 0, 0)
       myTotalTime = myTotalTime.Add(myTimePlayed)
       lblTimePlaying.Text = myTotalTime.Minutes.ToString("00") & ":" & myTotalTime.Seconds.ToString("00")

   End Sub

 

Error is on "Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)"

 

It says in the tooltip: Value of type 'Date' cannot be converted to 'System.TimeSpan'.

  • Leaders
Posted

Maybe you should be subtracting myPlaying time from Now in order to see how long your form has been open.

DateTime.Now.Subtract(myPlayingTime)

'This should return a timespan object when you subtract two date values.

 

Date + TimeSpan = Date,

Date - Date = TimeSpan

Date - TimeSpan = Date

:)

 

I wonder why you are adding a timespan to an empty timespan, though.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted (edited)

ok, now that i replaced the first add with subtract it now counts up, but i still have one problem.. its starting at whatever time the computer's clock is at.. like its 4:34 on my computer so it starts at 34:00

 

but otherwise its working fine..

Edited by Lanc1988
Posted
anyone? I'm sure I just have one small part of the code wrong.. I need it to start out with 00:00 instead of whatever the system's time is, which defeats the purpose of having a time form has been open.
Posted

so would this work?:

 

 

Load Event:

Timer1.Enabled = True
Dim PlayingTimeValue As String
PlayingTimeValue = DateTime

myPlayingTime = Date.Time - Date.Time

   Private myPlayingTime As DateTime

Timer Event:

   Private Sub TimePlaying_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimePlaying.Tick

       Dim myTimePlayed As TimeSpan = DateTime.Now.Add(myPlayingTime)
       Dim myTotalTime As New TimeSpan(0, 0, 0)
       myTotalTime = myTotalTime.Add(myTimePlayed)
       lblTimePlaying.Text = myTotalTime.Minutes.ToString("00") & ":" & myTotalTime.Seconds.ToString("00")

   End Sub 

  • Leaders
Posted

myPlayingTime = Date.Time - Date.Time

What is this supposed to do?

Subtracting something from itself will give you 0.

 

You should store DateTime.Now into a datetime variable at the start of your program.

 

Then, in the timer, you will subtract your current value from DateTime.Now.

DateTime.Now will be greater than the value that you have in the datetime variable, so you do: DateTime.Now - dtvar to get a timespan that has the difference of time. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
im so confused on what even to try... would it be possible for you to add that line to my code so it will work right.. I have been thinking of how to do this for days now.. :S
  • Leaders
Posted

It would probably be better for you to clean everything out and start from scratch, so that you can clear your mind.

 

Declare a DateTime variable: Dim D As DateTime

In the Load Event, you set D to the current time.

D = DateTime.Now

 

In the timer event you declare a timespan

Dim TS As Timespan

And then subtract D from DateTime.Now and put the result into timespan.

TS = D - DateTime.Now

Then, display the properties of the timespan anyway you want. (Your old way is fine, probably) :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

ok, thanks alot, works very nice now.. I have just one question.. Is there a way to put the 'Dim D As DateTime' in like a button so its just above the line that enables the timer?

 

I think that it would need to be a form wide variable somehow.. im not sure how to do something like that, cuz if I just move the Dim D As DateTime to the line just above the Timer1.Enabled = True then in the TimerTick event it has an error cuz the "D" isn't declared

  • Leaders
Posted

So, you don't know how to declare Dim D As DateTime as a form-wide variable?

 

You need to Dim D As DateTime outside of all of the subs.

So, find the first Private Sub in your form class, and put Dim D As DateTime on the lin before that. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
I have tried that before.. but the problem is, right when the form opens it is stores the time.. making my time player timer incorrect.. so I need it as a form-wide variable, yet it has to store the time like right as the timer is enabled.
  • Leaders
Posted

The Declaration shouldn't be storing the time. That comes from the line

D = Now

 

So, you need to move that line to the line immediately after you enable the timer. :)

This puts the time when the timer was enabled into the date variable.

Clearly, you couldn't have done this in the Timer event, as the Timer would reset this variable quite a few times.

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted
ok, the problem still is that when i delcare to store the time, it is under a sub for a menuitem.. so.. that means in the TimerTick events that it thinks "D" is not declared..
Posted

I like to declare my form level variables right after the "Windows Form Designer Generated Code statement."

 

Just declare any form level variables there the same as in a sub procedure. Such as:

 

Dim D As DateTime

 

Now any sub will have access to this variable. In a button click event, you could say something like:

 

D = DateTime.Now

 

Now this value will be in the form level variable, until it is either changed or the form is reloaded. Again any sub procedure will have access to this variable so you can do whatever you want to with it.

 

Chester

____________________________________________

http://www.pophamcafe.com

I am starting a developers section, more tutorials than anything.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...