Reputation: 17
I am making a test paper where counter is there. I have to continue it as user clicked on next button..
Code i used for timer on form 3 is:
Public Class Form3
Inherits System.Windows.Forms.Form
Private alarmTime As Date
Private Sub Form3_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.alarmTime = Date.Now.AddMinutes(60)
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Timer1.Tick
If alarmTime < Date.Now Then
Me.Timer1.Stop()
MessageBox.Show("Time's up.")
Else
Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)
Me.Label6.Text = String.Format("{0}:{1:d2}:{2:d2}", _
remainingTime.Hours, _
remainingTime.Minutes, _
remainingTime.Seconds)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frmDialogue As New Form8
frmDialogue.ShowDialog()
End Sub
Upvotes: 0
Views: 2055
Reputation: 353
You can create a main mdi window which will hold the timer and there will be sub windows in the which can access the parent timer or even the mdi itself can close all the test paper form and show msg box for time up when the time is up.
Upvotes: 0
Reputation: 2832
I would put all the code for your timer into a Module
. That way you can access it anywhere in your application. I would also add two new events Elapsed
and TimerComplete
so multiple objects and forms can subscribe to the key actions you wish to perform with when the timer ticks.
Here's the AppTimer
module that's accessible from anywhere:
Module AppTimer
Private alarmTime As Date
Private WithEvents timer As Timer = New Timer()
Public Sub Start(ByVal time as Date)
alarmTime = time
timer.Start()
End Sub
Public Sub TimerTick(ByVal sender As Object, ByVal e as EventArgs)
Handles timer.Tick
If alarmTime < Date.Now Then
timer.Stop()
RaiseEvent TimerComplete("Time's up.")
Else
Dim remainingTime As Timespan = alarmTime.Subtract(Date.Now)
RaiseEvent Elapsed(remainingTime)
End If
End Sub
'This will fire when the duration of the timer has elapsed
Public Event TimerComplete As Action(Of String)
'This will fire each time the timer ticks unless it's finished
Public Event Elapsed As Action(Of Timespan)
End Module
Now in Form3_Load(...)
you can set up the timer like so:
AppTimer.Start(Date.Now.AddMinutes(60))
And then in any form you wish to do something when these events fire, create two new methods and hook them up with AddHandler
:
AddHandler AppTimer.Elapsed, AddressOf HandleElapsed
AddHandler AppTimer.TimerComplete, AddressOf HandleComplete
Private Sub HandleElapsed(ByVal remainingTime As Timespan)
Me.Label6.Text = String.Format("{0}:{1:d2}:{2:d2}", _
remainingTime.Hours, _
remainingTime.Minutes, _
remainingTime.Seconds)
End Sub
Private Sub HandleComplete(ByVal message As String)
MessageBox.Show(message)
End Sub
It's also worth noting that you can hook up events with lambdas
:
AddHandler AppTimer.Elapsed, Sub(remainingTime)
String.Format("{0}:{1:d2}:{2:d2}", _
remainingTime.Hours, _
remainingTime.Minutes, _
remainingTime.Seconds)
End Sub
Hope this helps.
Upvotes: 1