Setting a time limit

skaryChinezeGuie

Well-known member
Joined
Apr 23, 2006
Messages
94
Programming Experience
Beginner
I started a timer at the form load event using

Public DiffZeit AsDate
Public Timer1 As Timer
(in the public variable module)

Timer1.Enabled = True
DiffZeit = DateTime.Now
( in the main form load event )

PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TSpan = DateTime.Now.Subtract(DiffZeit)
LblTime.Text = TSpan.Minutes & ":" & TSpan.Seconds
EndSub:D

but it still starts at 1 second and goes up. i need it to start at 30 min. and go backwards and stop at zero. Also, I would like to close the main form when it stops and open the " Results " form. Any help would be much appreciated.
 
Last edited:
Start with a timespan of 30 minutes and subtract the elapsed time, make sure it don't continue subtracting when minutes and seconds have reached zero.
VB.NET:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
 DiffZeit = Date.Now
 TSpan = New TimeSpan(0, 30, 0)
 Timer1.Interval = 1000
 Timer1.Enabled = True
End Sub
 
Public DiffZeit As Date
Public TSpan As TimeSpan
 
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Timer1.Tick
 If TSpan.Minutes > 0 Or TSpan.Seconds > 0 Then
  TSpan = TSpan.Subtract(Date.Now.Subtract(DiffZeit))
  DiffZeit = Date.Now
  lblTime.Text = TSpan.Minutes & ":" & TSpan.Seconds
 Else
  lblTime.Text = "time is out"
  Timer1.Enabled = False
 End If
End Sub
 
Back
Top