Question:
Timer/Time in Visual Basic (vb.NET)?
anonymous
2008-03-26 06:32:45 UTC
I'm working in vb.net and want to set up a timer that starts at zero and increases in one second intervals but I want the format to be "00:00:00" (hours:minutes:seconds). So after 75 seconds, for example, the timer will read "00:01:15"

Anyone know the correct way to achieve this?
Three answers:
anonymous
2008-03-26 08:32:58 UTC
I have designed and programmed a stopwatch in VB6 so that it resemble a typical digital stopwatch. When you click mode, you can select clock, date and stopwatch. When you select clock, the current time is displayed and when you select the date, the current date will be displayed. Lastly, when you select the stopwatch, all the digits will be set to 0 and now it can be used as a stop watch.



In this program, you need to insert one label, three command buttons and two timers. The interval of timer1 which is used for the stopwatch and you have to set the interval to 1. Timer2 will be used to display the clock and the interval will be set to 1000(or 1 second). I used 6 string variables to display the digits of the stopwatch so that I can put in the colons ":" and the decimal point. I also created a subroutine known as countime The codes are shown below:



Dim a As String

Dim b As String

Dim c As String

Dim x As String

Dim y As String

Dim z As String

Dim h As String

Dim m As String

Dim s As String

Dim u As String

Dim v As String

Public interval As Double







Private Sub clock_Click()

Timer1.Enabled = False

Timer2.Enabled = True

End Sub



Private Sub Command1_Click()



Timer1.Enabled = True

Timer1.interval = 1









End Sub



Private Sub Command2_Click()

Timer1.Enabled = False

End Sub



Private Sub Command3_Click()

Timer1.Enabled = False

a = "0"

b = "0"

c = "0"

x = "0"

y = "0"

z = "0"

u = "0"

v = "0"



h = a + b

m = c + x

s = y + z

'To set the display as "00:00:00.00"

Label1.Caption = h + ":" + m + ":" + s + "." + u + v

End Sub



Sub counttime()

If Val(v) < 9 Then

v = v + 1



Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(u) < 9 Then

v = 0

u = u + 1



Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(z) < 9 Then

v = 0

u = 0



z = z + 1



Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(y) < 5 Then

v = 0

u = 0

z = 0

y = y + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(x) < 9 Then

v = 0

u = 0

z = 0

y = 0

x = x + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(c) < 5 Then

v = 0

u = 0

z = 0

y = 0

x = 0

c = c + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(b) < 9 Then

v = 0

u = 0

z = 0

y = 0

x = 0

c = 0

b = b + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(b) < 9 Then

v = 0

u = 0

z = 0

y = 0

x = 0

c = 0

b = b + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v

ElseIf Val(a) < 9 Then

v = 0

u = 0

z = 0

y = 0

x = 0

c = 0

b = 0

a = a + 1

Label1.Caption = a + b + ":" + c + x + ":" + y + z + "." + u + v



End If



End Sub



Private Sub dat_Click()

Label1.Caption = Date

Timer2.Enabled = False

End Sub



Private Sub Form_Load()

a = "0"

b = "0"

c = "0"

x = "0"

y = "0"

z = "0"

u = 0

v = 0

h = a + b

m = c + x

s = y + z

'To set the display as "00:00:00.00"

Label1.Caption = h + ":" + m + ":" + s + "." + u + v



End Sub



Private Sub stopwc_Click()

Timer2.Enabled = False

a = "0"

b = "0"

c = "0"

x = "0"

y = "0"

z = "0"

u = "0"

v = "0"



h = a + b

m = c + x

s = y + z

Label1.Caption = h + ":" + m + ":" + s + "." + u + v



End Sub



Private Sub Timer1_Timer()

counttime



End Sub



Private Sub Timer2_Timer()

Label1.Caption = Time

End Sub



You need to modify the codes in VB.Net



For more info on Visual Basic, visit my free tutorial site at



http://www.vbtutor.net
J J
2008-03-26 13:58:48 UTC
Simply you would let the timer, un molested, run it's normal course.. then when it stops YOU do the conversion to the format you want.. the conversion is mundane division, right? That is the easiest and most transparent way of getting what you want...
tinytim
2008-03-29 07:59:21 UTC
U NEED TO PICK BEST ANSWER ON THIS 1


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...