Search code examples
vb.netwinformsdatetimepicker

How to add DateTimePicker value to Current Time


I'm having a difficult time with this. The program is an Alarm clock that takes a user input duration of time and adds the current time to get a label of the future time of when the alarm is finished.

Visual of the alarm here

I have accomplished removing hardcoded hours but I can't add the current time to the duration

Option Strict On
Public Class AlarmTimerFRM
    Private setTime As Date

    Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        outCurrentTimeLBL.Text = Date.Now.ToLongTimeString
        outCurrentTimeLBL.BackColor = Color.LightGray
        Timer1.Start()
        setTimerDTP.Format = DateTimePickerFormat.Custom
        setTimerDTP.CustomFormat = "HH:mm:ss"
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")

    End Sub
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        outCurrentTimeLBL.Text = TimeOfDay.ToString("hh:mm:ss tt")

    End Sub

    Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
        REM sets the alarm time 
        Dim OneHourAgo As DateTime
        Dim FormattedTime As String

        OneHourAgo = Now.AddHours(-1)
        FormattedTime = OneHourAgo.ToString("HH:mm:ss")

        AlarmTimeLBL.Text = (FormattedTime)

    End Sub

    Private Sub setTimerDTP_ValueChanged(sender As Object, e As EventArgs) Handles setTimerDTP.ValueChanged

    End Sub

    Private Sub ResetBTN_Click(sender As Object, e As EventArgs) Handles ResetBTN.Click
        REM Resets the alarm time LBL
        AlarmTimeLBL.Text = ""
    End Sub

    Private Sub AlarmTimeLBL_Click(sender As Object, e As EventArgs) Handles AlarmTimeLBL.Click

    End Sub
End Class

Solution

  • Your "setTime" variable should be a DateTime so that it includes the date. This makes it easier to deal with current times and durations that span midnight.

    Here's what it would like:

    Private setTime As DateTime
    
    Private Sub AlarmTimerFRM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UpdateCurrentTime()
        outCurrentTimeLBL.BackColor = Color.LightGray
        Timer1.Start()
        setTimerDTP.Format = DateTimePickerFormat.Custom
        setTimerDTP.CustomFormat = "HH:mm:ss"
    End Sub
    
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        UpdateCurrentTime()
    End Sub
    
    Private Sub UpdateCurrentTime()
        outCurrentTimeLBL.Text = DateTime.Now.ToString("hh:mm:ss tt")
    End Sub
    
    Private Sub SetBTN_Click(sender As Object, e As EventArgs) Handles SetBTN.Click
        setTime = DateTime.Now.Add(setTimerDTP.Value.TimeOfDay)
        AlarmTimeLBL.Text = setTime.ToString("hh:mm:ss tt")
        SetBTN.Enabled = False
        setTimerDTP.Enabled = False
        Timer2.Start()
    End Sub
    
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        If DateTime.Now >= setTime Then
            Timer2.Stop()
            MessageBox.Show("Alarm time reached!")
            SetBTN.Enabled = True
            setTimerDTP.Enabled = True
        End If
    End Sub