Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is what my form looks like: https://pasteboard.co/JjermF5.png[^]

The problem im having with my code is that when I set values using the NumericUpandDown its not reflecting and starting a timer when i set the time and click start, and is also not allowing me to not add a value in the minute NumericUpandDown box.

Im unable to identify the error, can someone please help
The code is below:

What I have tried:

Public Class btnStandardSet
    Dim temp As Long
    Private Sub btnStandardSet_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetDefaults()
    End Sub

    Private Sub SetDefaults()
        timerStandard.Enabled = False
        btnSPCStandard.Enabled = False
        btnStopStandard.Enabled = False
        btnSetStandard.Enabled = True
        cmbStandardHour.Enabled = True
        cmbStandardSec.Enabled = True
        lblStandardHourValue.Text = "00"
        lblStandardMinValue.Text = "00"
        lblStandardSecValue.Text = "00"
        temp = 0
    End Sub

    Private Sub SetTime()
        lblStandardHourValue.Text = temp / 3600
        lblStandardMinValue.Text = (temp Mod 3600) / 60
        lblStandardSecValue.Text = temp Mod 3600 Mod 60
        If lblStandardHourValue.Text = 1 Then
            lblStandardHourValue.Text = "0" & lblStandardHourValue.Text
        End If
        If lblStandardMinValue.Text = 1 Then
            lblStandardMinValue.Text = "0" & lblStandardMinValue.Text
        End If
        If lblStandardSecValue.Text = 1 Then
            cmbStandardSec.Text = "0" & cmbStandardSec.Text
        End If
    End Sub

    Private Sub btnSetStandard_Click(sender As Object, e As EventArgs) Handles btnSetStandard.Click
        If cmbStandardMin.Value = "0" Then
            MsgBox("Please set a valid duration1")
        Else
            temp = cmbStandardHour.Value * 3600 + cmbStandardMin.Value * 60 + cmbStandardSec.Value
            cmbStandardHour.Enabled = False
            cmbStandardMin.Enabled = False
            cmbStandardSec.Enabled = False
            btnSetStandard.Enabled = False
            btnSPCStandard.Enabled = True
        End If
    End Sub

    Private Sub timerStandard_Tick(sender As Object, e As EventArgs) Handles timerStandard.Tick
        If temp = 0 Then
            MsgBox("Time is Over")
        Else
            temp = -1
            SetTime()
        End If
    End Sub

    Private Sub btnSPCStandard_Click(sender As Object, e As EventArgs) Handles btnSPCStandard.Click
        If btnSPCStandard.Text = "Start" Then
            SetDefaults()
            timerStandard.Enabled = True
            btnSPCStandard.Text = "Pause"
            btnStopStandard.Enabled = True
        ElseIf btnSPCStandard.Text = "Pause" Then
            timerStandard.Enabled = False
            btnSPCStandard.Text = "Stop"
        ElseIf btnSPCStandard.Text = "Continue" Then
            btnSPCStandard.Text = "Pause"
            timerStandard.Enabled = True
        End If
    End Sub

    Private Sub btnStopStandard_Click(sender As Object, e As EventArgs) Handles btnStopStandard.Click
        SetDefaults()
    End Sub
End Class
Posted
Updated 26-Jul-20 4:28am
Comments

Look at your code:
Private Sub timerStandard_Tick(sender As Object, e As EventArgs) Handles timerStandard.Tick
    If temp = 0 Then
        MsgBox("Time is Over")
    Else
        temp = -1
        SetTime()
    End If
End Sub

Private Sub SetTime()
    lblStandardHourValue.Text = temp / 3600
    lblStandardMinValue.Text = (temp Mod 3600) / 60
    lblStandardSecValue.Text = temp Mod 3600 Mod 60
    If lblStandardHourValue.Text = 1 Then
        lblStandardHourValue.Text = "0" & lblStandardHourValue.Text
    End If
    If lblStandardMinValue.Text = 1 Then
        lblStandardMinValue.Text = "0" & lblStandardMinValue.Text
    End If
    If lblStandardSecValue.Text = 1 Then
        cmbStandardSec.Text = "0" & cmbStandardSec.Text
    End If
End Sub

SetTime modifies the display from temp - but your tick event always sets temp to -1. So the value displayed will always be ... um ... garbage.

To do a countdown timer, the simplest way is:
Set up a Timer with an interval of half a second. Start it.
Set up a DateTime value: call it expires and set it to DateTime.MinValue.
In your Tick event, check if expires equals DateTime.MinValue.
If it does, do nothing.
If it doesn't, check it against DateTime.Now
If it's greater, use your SetTime method (modified) to display the difference (Hint: you can subtract two DateTime values to give you a Timespan which gives you hours, minutes, and seconds)
If it's smaller, the timer has elapsed, so set expires back to DateTime.MinValue.

Start and Stop operation for you app consist of setting the value of expires: DateTime.MinValue to stop it,
DateTime.Now.AddMinutes(30) to wait half an hour.

Give it a try: it's a lot easier than it looks!
 
Share this answer
 
v2
I'm not sure wether temp contain a valid value - at this point you have to do something. Also I can't see how temp gets it's value.
But ... here you have to change something :
VB
Private Sub timerStandard_Tick(sender As Object, e As EventArgs) Handles timerStandard.Tick
    If temp <= 0 Then   // <----- compare not only to 0
        MsgBox("Time is Over")
    Else
        temp -= 1   //  <----- decrease the value from temp (not set it to -1)
        SetTime()
    End If
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900