Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code a count down on a form

Status
Not open for further replies.

paulcy82

Programmer
Jan 25, 2005
28
0
0
US
I need help coding a count down on a form. I want it to count down from 5 minutes. I have a timer on my form that is enabled on load and runs a function which tests a SQL statement. If this statement is true I print reports, if it is false, I want a count down of 5 minutes to be displayed on the form. The count down is when the next test of the SQL will be.
 
here's one solution

Declare a modular variable to hold the finishing time...

Private m_oTimeEnd As Date

Then to start add in this code in whatever event is supposed to begin the count...

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
m_oTimeEnd = Now.AddMinutes(5)
Timer1.Enabled = True

End Sub

Lastly add the timer tick code...

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim l_oCurrentTime As Date = Now
Dim l_oTimeDifference As TimeSpan = m_oTimeEnd.Subtract(l_oCurrentTime)

Label1.Text = l_oTimeDifference.ToString

End Sub

you might want to change the format so that it doesn't show zero hours but I can never remember whether it's M or m for minutes. Probably M for months and m for minutes.
 
Thank you Jubble for the example, I used your code and I ended up with the following

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Dim l_oCurrentTime As Date = Now

Dim l_oTimeDifference As TimeSpan = m_oTimeEnd.Subtract(l_oCurrentTime)
Me.lblCt.Text = l_oTimeDifference.ToString
Debug.Write(l_oTimeDifference)
If l_oTimeDifference.TotalSeconds < 0 Then
lblCt.Text = "Checking Status..."
Debug.Write("Checking Status...")
Application.DoEvents()
mainFunct()
End If
Application.DoEvents()
End Sub

When I load the Form a funct is called that checks the status of a database. Once this is returned, if the database is not updated, a timer is set to true and the above code is ran on tick, once the timer is out, the app checks the DB again, etc.

Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top