I need help with timer code

El Don

Member
Joined
Feb 19, 2009
Messages
8
Programming Experience
Beginner
I'm currently writing a countdown timer on vb 2005, with the following characteristics:

-When I press a button and the timer is running, it adds time to the counter, for example, if the time is 2:30 and I press a button, and the period to add is 1:10, the new time would be 4:40.

-The window is always on top of every other window, and is active even though it doesn't have focus.

-When the time's up and i press a key, in this case Esc, the application closes, otherwise, it just continues.

So far, I have all the settings and the timer, but I can't seem to get this three parts right, here's my code, if you could help me, that would be great.
Here's my code
VB.NET:
Public Class Form1
    Declare Function SetWindowPos Lib "user32" ( _
        ByVal hwnd As Long, _
        ByVal hWndInsertAfter As Long, _
        ByVal x As Long, _
        ByVal y As Long, _
        ByVal cx As Long, _
        ByVal cy As Long, _
        ByVal wFlags As Long _
    ) As Long
    Public Const HWND_TOPMOST = -1
    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1
    Public Const swp_showwindow = &H2
    Public Const swp_noactivate = &H1

    Private alarmTime As Date

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Label1.Text = String.Format("press to run")
        Me.CenterToScreen()
        Me.TopMost = True
    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()
            Me.Label1.Text = String.Format("time's up")
            My.Computer.Audio.Play("c:\DING.WAV")
        Else
            Dim remainingTime As TimeSpan = Me.alarmTime.Subtract(Date.Now)

            Me.Label1.Text = String.Format("{0}:{1:d2}:{2:d2}", _
                                           remainingTime.Hours, _
                                           remainingTime.Minutes, _
                                           remainingTime.Seconds)
        End If
        If Me.Label1.Text = String.Format("0:00:10") Then
            Me.CenterToScreen()
            Me.Label1.ForeColor = Color.Yellow
        End If
        If Me.Label1.Text = "0:00:05" Then
            Me.Label1.ForeColor = Color.Red

        End If
    End Sub

    Private Sub Button1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown

        If e.KeyCode = Keys.A Then
            Me.alarmTime = Date.Now.AddMinutes(0.7)
            Me.Timer1.Start()
            My.Computer.Audio.Play("c:\CHIMES.WAV")
            Me.SetDesktopLocation(0, 745)
            Me.Label1.ForeColor = Color.White
        End If

        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub
    Public Sub SetFormPosition(ByVal hWnd As Long, ByVal Position As Long)
        SetWindowPos(hWnd, Position, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or swp_showwindow Or swp_noactivate)
    End Sub
End Class

thanks in advance
 
How about:

remainingTime = remainingTime.Add(New TimeSpan(0,1,0)
This would add 1 minute to it. If you need to use a Format like 00:01:10
then in the button event ->
Dim myArr as Array = label1.text.spilt(":")
remainingTime = remainingTime.Add(New TimeSpan(myArr(0), myArr(1), myArr(2))
This will set the 00:01:10 as ( 00 , 01 , 10 )
 
Last edited:
thanks a lot, now it adds time!

Its now better than before, that leaves two things:

- the form being on top and active without focus, and

- to close with the press of key only if time is up.


thank you very much for your help.
 
No problem, glad to help. If you need more help, please post new questions on a new thread...
 
Sorry to bother man, I lost the code, and I don't remember where exactly I wrote what you suggested, can you tell me where it goes?

Right now I'm getting 'System.NullReference.Exception'
 
Last edited:
I would have to guess, this code goes in a button_click event that you want to do this work. But it could go somewhere else too. This code just updates the label1.text like the .subtract nethod does during the tick event, except this is a one time update. My code pulled the label.text - what looked like the control holding the timer value, then used your TimeSpan to update it.
 
Back
Top