How to add DateTimePicker value to Current Time - vb.net

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.
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

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

You could plus DateTime.Now with User Input Time.
Dim UserTime As String()
UserTime = Duration.Text.Split(':')
AlarmTimeLBL.Text = DateTime.Now.AddHours(UserTime(0)).AddMinutes(UserTime(1)).AddSeconds(UserTime(2))

Related

StopWatch and Timer Accuracy

I'm using the following code to control a label to output a stopwatch. The buttons work, and the label is outputting the information mostly correctly, however it only update every so often and i would like for the label to update the information every millisecond.
Private SW As New Stopwatch
Dim timercount As Integer = 1 'The number of seconds
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
SW.Start()
Timer1.Start()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer1.Enabled = False 'Stop the timer
timercount = 0 'Reset to 0 seconds
Label10.Text = "00:00:00.000"
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim ts As TimeSpan = SW.Elapsed
Label10.Text = ts.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Timer1.enabled = False
End Sub
Any help is much appreciated.
You can set the timer to a reasonable value, say 20 times a second, to rapidly display it changing.
You can create the timer and stopwatch in code, which I think is easier than adding a control to the form—you can see right there in the code what is being programmed. I used sensible names for the buttons so that you can tell what is meant to do what.
If you keep the sub to display the data separate from the code that processes the data, it is easy to change the display in just one place, in case someone decided they wanted it written as words, for example, or maybe the hours in a separate box, or something.
Public Class Form1
Dim sw As New Stopwatch
Dim tim As Timer
Private Sub ShowElapsedTime()
lblTime.Text = sw.Elapsed.ToString("hh\:mm\:ss\.fff")
End Sub
Private Sub bnStart_Click(sender As Object, e As EventArgs) Handles bnStart.Click
sw.Restart()
tim.Enabled = True
End Sub
Private Sub bnStop_Click(sender As Object, e As EventArgs) Handles bnStop.Click
sw.Stop()
tim.Enabled = False
ShowElapsedTime()
End Sub
Private Sub bnReset_Click(sender As Object, e As EventArgs) Handles bnReset.Click
tim.Enabled = False
sw.Reset()
ShowElapsedTime()
End Sub
Private Sub tim_Tick(sender As Object, e As EventArgs)
ShowElapsedTime()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tim = New Timer With {.Interval = 50}
AddHandler tim.Tick, AddressOf tim_Tick
ShowElapsedTime()
End Sub
End Class

I have this countdown timer and i want it to start from a number set in a variable

I have this count down timer and i want it to start to count down from a value given in a variable.
Public Class frmSinglePlayer
Private TargetDT As DateTime
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
Private Sub frmSinglePlayer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmrCountdown.Interval = 500
TargetDT = DateTime.Now.Add(CountDownFrom)
tmrCountdown.Start()
End Sub
Private Sub tmrCountdown_Tick(sender As Object, e As System.EventArgs) Handles tmrCountdown.Tick
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
tmrCountdown.Stop()
MessageBox.Show("Done")
End If
End Sub
End Class
I want to change instead of the number 3 in this line, to put the value from my variable.
Private CountDownFrom As TimeSpan = TimeSpan.FromMinutes(3)
I have 3 radiobuttons with different values.
Private Sub rd_1h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_1h.CheckedChanged
ho = 1
End Sub
Private Sub rd_2h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_2h.CheckedChanged
ho = 2
End Sub
Private Sub rd_3h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_3h.CheckedChanged
ho = 3
End Sub
If is confusing , please ask me and sorry for my bad english.
Maybe you need something like this?
Private TargetDT As DateTime
Dim ho As Integer = 1
Private Sub StartTimerTick(sender As Object, e As System.EventArgs, Optional countDownFrom As Integer = 0)
If sender Is Nothing Then
TargetDT = DateTime.Now.Add(TimeSpan.FromMinutes(countDownFrom))
Dim mTmr = New Timer
mTmr.Interval = 500
AddHandler mTmr.Tick, AddressOf StartTimerTick
mTmr.Start()
Exit Sub
End If
Console.WriteLine("Tick ")
Dim ts As TimeSpan = TargetDT.Subtract(DateTime.Now)
If ts.TotalMilliseconds > 0 Then
lblTime.Text = ts.ToString("mm\:ss")
Else
lblTime.Text = "00:00"
CType(sender, Timer).Stop()
CType(sender, Timer).Dispose()
MessageBox.Show("Done")
End If
End Sub
Private Sub rd_1h_CheckedChanged(sender As Object, e As EventArgs) Handles rd_1h.CheckedChanged,
rd_2h.CheckedChanged,
rd_3h.CheckedChanged
Select Case CType(sender, Control).Name
Case "rd_1h"
ho = 1
Case "rd_2h"
ho = 2
Case "rd_3h"
ho = 3
End Select
'StartTimerTick(Nothing, Nothing, ho)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
StartTimerTick(Nothing, Nothing, ho)
End Sub

Enable a counter at a specific time everyday

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

I'm trying to open a form and getting this error

Okay, here is my code.
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ProgressBar1.Minimum = 0
ProgressBar1.Maximum = 1000
ProgressBar1.Value = 1000
Timer1.Interval = 1750
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Form3.Show()
Me.Close()
End Sub
End Class
On the line Form3.Show() I get
InvalidOperationException was unhandled.
An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.
What I have on Form3:
Public Class Form3
Public IPAddress As String = TextBox1.Text
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form4.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Form4.varible1 = True Then
Label1.Text = "IP: " + IPAddress
End If
End Sub
End Class
Any help?
Change on form3
Public IPAddress As String = TextBox1.Text ...."TextBox1.Text is "" thats why you get the error"
To
Public IPAddress As String
End then add IPAddress = TextBox1.Text in you timer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Form4.varible1 = True Then
IPAddress = TextBox1.Text
Label1.Text = "IP: " + IPAddress
End If
End Sub

VB.net real-time time elapsed feature

I already have created a real time clock that synchronizes with the computer time and is being displayed in a label.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
I want to make a real-time time elapsed feature that keeps on counting the seconds/minutes/hours elapsed from the time it started till the time it stops and it would be basing on the real-time clock i have created. I would be creating a start and stop button for this. Is this possible? Thanks in advance.
I am now able to complete everything and i added a feature that records the starting and ending time based on my real time clock. Here is my working code:
Dim hr, min, sec As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Start.Text = ""
EndLbl.Text = ""
Elapse.Text = ""
Timer2.Enabled = True
Start.Text = Time.Text
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
sec = sec + 1
If (sec = 60) Then
sec = 0
min = min + 1
ElseIf (min = 60) Then
min = 0
hr = hr + 1
ElseIf (hr = 24) Then
hr = 0
min = 0
sec = 0
End If
Elapse.Text = String.Format("{0}hr : {1}min : {2}sec", hr, min, sec)
Timer2.Interval = 1000
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Timer2.Enabled = False
EndLbl.Text = Label4.Text
hr = 0
min = 0
sec = 0
Timer2.Interval = 1
End Sub
Credits to the starting code given by NeverHopeless. Thanks alot.
I suggest you use only 1 timer:
Public Class Form2
Private _elapseTimerRunning As Boolean = False
Private _elapseStartTime As DateTime
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles Me.Load
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
txtTime.Text = Now.ToString("h:mm:ss tt")
If _elapseTimerRunning = True Then
Dim elapsedtime = DateTime.Now.Subtract(_elapseStartTime)
txtElapsed.Text = String.Format("{0}hr : {1}min : {2}sec", elapsedtime.Hours, elapsedtime.Minutes, elapsedtime.Seconds)
End If
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
_elapseStartTime = DateTime.Now
_elapseTimerRunning = True
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
_elapseTimerRunning = False
End Sub
End Class
An example for displaying the elapsed time the application has run.
Public Class Form1
'shows elapsed time that the app has run
'
Dim elapTimer As New Threading.Timer(AddressOf tick, Nothing, 1000, 1000)
Dim stpw As Stopwatch = Stopwatch.StartNew
Private Sub tick(state As Object)
If stpw.IsRunning Then
'format - http://msdn.microsoft.com/en-us/library/ee372287.aspx
Me.Invoke(Sub()
Label1.Text = stpw.Elapsed.ToString("d\ \ hh\:mm\:ss\.ff")
End Sub)
End If
End Sub
End Class
To add start/stop functionality using buttons:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'start the elapsed timer
stpw.Start() 'continue
'or
'stpw.Restart() 'restart
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stpw.Stop()
End Sub
Something like this would help you: (untested, but will give you a starter)
Dim hr, min, sec As Integer 'msec;
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'msec++;
'if(msec == 60) { msec = 0; sec++; }
sec+=1;
if(sec = 60) Then
sec = 0 : min+=1
end if
if(min = 60) Then
min = 0 : hr+=1
end if
if(hr = 24) Then
hr = 0 : min = 0 : sec = 0
end if
'TimeElapsed.Text = String.Format("{0}:{1}:{2} {3}", hr, min, sec, msec)
TimeElapsed.Text = String.Format("{0}:{1}:{2}", hr, min, sec)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Time.Text = Date.Now.ToString("h:mm:ss tt")
End Sub
NOTE: Timer2 will run for every second.