tags:

views:

35

answers:

2

Hi everybody (hi Dr.Nick!) I'm trying to create a pretty simple program that basically is a timer. I have three sets of labels, lbl_seconds, lbl_minutes and lbl_hours. These labels have the default value of 00:00 and I want the timer to change that for each label. I have googled this but I cannot seem to find any good info on it. Do I need three separate timers? I have also noticed that the timers have their own tick event handler. I guess it's in this that I need to change the value of the label. But I cannot figure out how to do just that. Any quick ideas?

A: 

Use one timer and in event sub change value of your labels.

You need one timer and three counter for seconds, minutes and hours.

Count minutes, then modulo minutes / 60, if return 0 then start count minutes. Modulo minutes/60, if return 0 then start count hours.

bswietochowski
I've figured out that much myself, as stated in the first post. I've more interested in what the code might look like. I mean, I use a text value of "00:00" in the labels. Which can't be converted to Double just like that, I haven't been able to this far atleast.
Kenny Bones
In timer tick try to type
bswietochowski
+1  A: 

I think you need something of this sort

Public Function GetTime(Time as Integer) As String
    Dim Hrs        As Integer  'number of hours   '
    Dim Min        As Integer  'number of Minutes '
    Dim Sec        As Integer  'number of Sec     '

    'Seconds'
    Sec = Time Mod 60

    'Minutes'
    Min = ((Time - Sec) / 60) Mod 60

    'Hours'
    Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60

    Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function

You pass the time (in seconds) you'd like to display on the label's text and the time will be formatted as you like it.

e.g.

lblTime.Text = GetTime(90)

This will display 00:01:30 on the label.


For reference, you can see this project I submitted on FreeVBCode some time ago. The only caveat is the project is in VB6. You should be able to open it in Visual Studio though.

Alex Essilfie
This was exactly what I was looking for. I managed to mix up some code that works, but this is smooth :)
Kenny Bones
Sorry, one more question. This Function is for formatting only? It doesn't actually do any counting? I'm just wondering what I should put in the timer_Tick event. Displaying the label in hours:minutes:seconds is all well. But I still want it to actually count as well, using the tick event. I'll look at your project as well though, perhaps get some clues there. Edit: I think I solved it. I declared a global variable as integer and set this to the value of 0 on form_load event. Then in the tick event I set this value to itself + 1. Seems to work just fine :)
Kenny Bones
Rightly said. The function does formatting only. You'll have to use a variable to store the time and then use a `Timer`'s `Tick` event to control the time (increase/decrease). You'll also have to put in your `Timer.Tick` event, a method call to display the time on the label(s).
Alex Essilfie