Making a clock which would start running from specific time - vb.net

I'm trying to make a clock which would start running from a specific time - e.g. the user sets the time to be 17.35 and it runs from there. What would be the easiest way to do it? I tried setting the time with Timeserial but couldn't figure out how to add time to it so it didn't get me anywhere.
Ideas?
edit: The idea behind the program is to show the user a normal digital clock that has been sped up.

Add a Label and a Timer component in your form and set the starting date and time (the date won't be visible). So if you set 17:35:00 the time will start from that moment and be updated every second like a clock.
Public Class Form1
Dim startTime As DateTime
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
startTime = New DateTime(2014, 1, 1, 17, 35, 0) 'setting time at 17:35:00
Label1.Text = startTime.ToString("HH:mm:ss")
Timer1.Interval = 1000 '1 tick every second
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
startTime = startTime.AddSeconds(1)
Label1.Text = startTime.ToString("HH:mm:ss")
End Sub
End Class

Create a form with a timer on it. Set the timer to 1000ms and enable it.
Dim three variables hours, mins, secs. On the timer event, increment the secs. When secs = 60, set secs = 0 and increment the mins; ditto mins to hours, then display the hrs:mins:secs in a format of your choice. Add a button which allows the user to enter starting values for hrs, mins, secs.
Depending on what you mean by 'sped up' you could reduce the delay on the timer if you want it to run faster, as opposed to ahead of local time.

Related

How to enable/ disable the button if the time reaches the desired time?

I have 2 date time picker(dtpDate & dtpTime) that show the current date and time. I also have a button labeled time-out, what I want to do is if the dtpTime reaches 12:00 noon I want the time-out button to be enable. Thanks in advance!
You need to handle Events because the dateTimePicker's value may change.
In the following code, I handle dtpTime.ValueChanged and MyBase.Load
Private Sub dtpTime_ValueChanged(sender As System.Object, e As System.EventArgs) Handles dtpTime.ValueChanged, MyBase.Load
If dtpTime.Text = "12:00:00" Then
' Enable your button
btnTimeOut.Enabled = True
'MsgBox("Hello")
End If
End Sub

display a message to a label in vb.net at specified times during the day

I would like to display a sentence in a text box at a 5 specific times during the day automatically. for example:
at 5:30 AM,
Textbox1.text = "breakfast"
at 7:30 AM
textbox1.text = "leave for school",
etc.
a timer can just start when the application is launched, although it needs to refer to the local time or some constant time as the program needs to output at the same time each day of the week without me having to change it manually.
The proper way to do it is something like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Set the interval at startup.
Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Set the interval at each notification.
Timer1.Interval = CInt(GetNextNotificationTime().Subtract(Date.Now).TotalMilliseconds)
'Not sure whether this is required when the Interval changes or not.
Timer1.Stop()
Timer1.Start()
'Do the work here.
End Sub
Private Function GetNextNotificationTime() As Date
'...
End Function
How you implement that GetNextNotificationTime method depends on how the notification times are stored. The Timer will then Tick only when a notification is due.
You can still do this with a Timer and you wouldn't have to do any math...
Every time the Timer raises a Tick event, you check the value of: System.DateTime.Today.Now.ToString("HH:mm"). If it is equal to your preset time, change the text in the TextBox

Show only relevant numbers from stopwatch

While attempting to make a stopwatch, I noticed that it would display all groups of numbers related to the timer (in this case "00:00:00:00")
In order to show only relevant numbers, and not show the minutes column when a minute hadn't passed, I came up with this code:
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim sw As New Stopwatch
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Label1.Text = String.Format("{0:00}:{1:00}",
Math.Floor(elapsed.Seconds),
elapsed.Milliseconds)
If Label1.Text = "60:999" Then
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}",
Math.Floor(elapsed.Minutes),
elapsed.TotalSeconds, elapsed.TotalMilliseconds)
End If
End Sub
When this code is active, the timer will only show the seconds and milliseconds column until it hits a full minute, in which case it will just loop back to 0 seconds and repeat. I'm assuming that the timer just can't detect exactly when label1's text is exactly 60.999, but I'm not sure. What is my logic missing?
Timers are not guaranteed to fire exactly at their interval. I'm assuming your timer is set to go off every 1ms, however when you run your program you'll find it will probably by raised every 3-16ms with a lot of jitter, this is also not helped by the fact the WinForms Timer (which I assume you're using) goes through the Win32 window message pump, rather than its own dedicated (and real-time) thread.
Anyway, the fix is to not compare strings, instead compare the actual time values:
If elapsed.TotalSeconds < 60 Then
label1.Text = String.Format("{0:00}:{1:00}", Math.Floor(elapsed.Seconds), elapsed.Miliseconds)
Else
label1.Text = String.Format("{0:00}:{1:00}:{2:00}", Math.Floor(elapsed.TotalMinutes), Math.Floor(elapsed.Seconds), elapsed.Miliseconds)
End If
Note you shouldn't be displaying TotalSeconds if you're already displaying Minutes.
There's a few problems with your code:
You are declaring a new Stopwatch inside of the Tick event. This is unnecessary.
You're using string comparisons to do something that should be done with math. Your hunch is correct that the exact moment when the label's text is "60:999" is being missed by the timer.
Instead of comparing the label's text value, just look at how many milliseconds (or seconds) have elapsed!
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
If elapsed.TotalSeconds >= 60 Then
Label1.Text = String.Format("{0:00}:{1:00}:{2:00}",
Math.Floor(elapsed.Minutes),
Math.Floor(elapsed.Seconds),
elapsed.Milliseconds)
Else
Label1.Text = String.Format("{0:00}:{1:00}",
Math.Floor(elapsed.Seconds),
elapsed.Milliseconds)
End If
End Sub

Timer minutes is always 30 seconds out

Goodday, I use the below code to start a timer .
Label2.Text = Difference1.TotalMinutes.ToString("N0")
But the minutes are always 30 seconds out. The label shows the time as 1 minute when only 30 seconds have elapsed and thereafter I'm always 30 seconds out.
How can I calibrate this?
Thanks
Rob
If you look at the definition for the TimeSpan.TotalMinutes property it states that it:
Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.
Therefore when you use the ToString("N0") format you are telling it that you want no decimal places and since it is a numeric format it will round your value up. You should look at using the TimeSpan Custom Formats in particular in this case the %m Custom Format string. It should look something like this:
Label2.Text = Difference1.TotalMinutes.ToString("%m")
Code I used to test. Timer interval is set to 1000 and is enabled.
Public Class Form1
Dim startTime As DateTime = DateTime.Now
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = (DateTime.Now - startTime).ToString("%m")
Label2.Text = (DateTime.Now - startTime).TotalSeconds.ToString("N0")
End Sub
End Class

Detect if mouse stop moving vb.net

I would like to stop the timer whenever a mouse stops moving inside a groupbox
As of now, I start the timer when the mouse hover at the groupbox and stops it when it leaves the group box.
Private Sub gbxMouseMap_MouseHover(sender As Object, e As System.EventArgs) Handles gbxMouseMap.MouseHover
Timer.Start()
End Sub
Private Sub gbxMouseMap_MouseLeave(sender As Object, e As System.EventArgs) Handles gbxMouseMap.MouseLeave
Timer.Stop()
End Sub
In the MouseMove event set a class varible named LastMoveTime to the current timer elapsed time. In the MouseHover event check to see if LastMoveTime has reached the timeout period, if so stop the timer.
I will get you started...
Private LastMoveTime As DateTime
Private MouseTimeoutMilliseconds as Integer = 500
'put inside hover
If LastMoveTime.AddMilliseconds(MouseTimeoutMilliseconds) < Now Then
Timer.Stop()
Else
Timer.Start()
End if
To prevent having to handle this for many controls you can rearrange things a bit and cache the information needed to know if cursor has moved and how long the idle time is, to do this you need a Point variable and a Date variable. The Timer needs to tick all the time. In addition, to balance the cursor Show/Hide calls you need a variable to keep track of its visibility state. Here is the complete code sample:
Private loc As Point, idle As Date, hidden As Boolean,
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If loc <> Cursor.Position Then
If hidden Then
Cursor.Show()
hidden = False
End If
loc = Cursor.Position
idle = Date.Now
ElseIf Not hidden AndAlso (Date.Now - idle).TotalSeconds > 3 Then
Cursor.Hide()
hidden = True
End If
End Sub
This Timer can tick each 1/2-1 seconds depending on how responsive you want it, the idle time is set to 3 seconds. The code should be easy to understand when you read it and give it some thought, if not ask