Create a simple timer to count seconds, minutes and hours
Solution 1
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.
Solution 2
Here is an example of this
Dim timercount As Integer = 60 'The number of seconds
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Timer1.Interval = 1000 'The number of miliseconds in a second
Timer1.Enabled = True 'Start the timer
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Timer1.Enabled = False 'Stop the timer
timercount = 60 'Reset to 60 seconds
lblOutput.Text = timercount.ToString() 'Reset the output display to 60
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lblOutput.Text = timercount.ToString() 'show the countdown in the label
If timercount = 0 Then 'Check to see if it has reached 0, if yes then stop timer and display done
Timer1.Enabled = False
lblOutput.Text = "Done"
Else 'If timercount is higher then 0 then subtract one from it
timercount -= 1
End If
End Sub
Solution 3
Start off by adding a timer. Call it whatever you like, in this example I will be keeping it as Timer1. Add a label and set the text as: 00:00.
In the code after the class has been set (usually it is Public Class Form1) make a variable as a stopwatch: Dim stopwatch As New Stopwatch
In the timer tick event code, put the following: (Please note that my 00:00 label is called Label1)
Label1.Text = String.Format("{0}:{1}:{2}", watch.Elapsed.Hours.ToString("00"), watch.Elapsed.Minutes.ToString("00"), watch.Elapsed.Seconds.ToString("00"))
Kenny Bones
Updated on August 14, 2020Comments
-
Kenny Bones over 3 years
I'm trying to create a pretty simple program that basically is a timer. I have three sets of labels,
lbl_seconds
,lbl_minutes
andlbl_hours
. These labels have the default value of00: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. How to do just that?
-
jangeador over 13 yearsDo all three labels have the default 00:00 or is each label initialized to 00? It seems to me that you only need one timer and then you need to have 3 global counters to keep track of seconds, mins, and hours.
-
Kenny Bones over 13 yearsThanks :) Yeah, each label has the default value of 00:00 and this makes it harder to parse the string to Double it seems. Probably because of the :. Now, could this be separated into two labels? Seconds and minutes? Instead of one label with the value of 00:00 ?. And what do you mean about Global counters? Not familiar with that I think.
-
jangeador over 13 yearsI am not sure that global counters is the correct term, I meant that you do not need to parse the string, just use variables to keep track of h, m and s and on each tick event increment those variables. You can then change the label values to the value of each of the variables. You can use String.Format to format the variables in any way you wish.
-
Kenny Bones over 13 yearsHmm, I just tried to create a separate label for minutes and a separate one for hours. Although I could probably merge them into one label and then format the string I guess. But I think I'll go for this approach really. I generally just do a simple check of the values of the labels. For each tick which is every minute, the value of the label_minute is itself + 1. Then I do an If statement. If label_minute > 59 Then label_hour is itself + 1. Seems to be working pretty well actually. :)
-
-
Kenny Bones over 13 yearsI'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 over 13 yearsThis was exactly what I was looking for. I managed to mix up some code that works, but this is smooth :)
-
Kenny Bones over 13 yearsSorry, 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 :)
-
Alex Essilfie over 13 yearsRightly said. The function does formatting only. You'll have to use a variable to store the time and then use a
Timer
'sTick
event to control the time (increase/decrease). You'll also have to put in yourTimer.Tick
event, a method call to display the time on the label(s).