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

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

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

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

Visual Basic adding number loop

I am trying to make an idle game something like cookie clicker, and I am having problems with making a simple line of code that repeats every second and adds 5 to a number every second. Could anyone help me? I want this to start the loop if someone clicks the button.
you could use a timer. Enable/start the timer when the button is clicked.
See example at MSDN: Windows Form Timer
add a timer and in the button code type :
TimerName.start
and add this in the timer code :
TimerName.interval = 1000
'replace TimerName with the name of timer you just added
'this will add 5 to number you want every second , interval of timer = 1000 that means it does the code every second
NumberThatYouWant += 5
'Replace NameThatYouWant with the number name that you want to add 5 to it every second
Yeah create a timer, then set the timer.interval to 1000 to tick each second, then create a sub for timer.tick and put the number you want to be increased in there and that should work.
EG.
Private Sub Timer1_Tick() Handles Timer1.Tick
variable += 5
End Sub
You have to change the interval in the properties window (bottom right)
Hope this helps!
Edit: I didn't include Timer1.start because the other answers said that. Don't forget to use it.
Setting the Timer
First, add a timer control to your form. Set the timer's interval value to '1000' (the timer's interval is measured in milliseconds). You should also enable your timer at run-time:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
So your code should look similar to this:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
End Sub
Adding the value with the Timer
Now say the button's name is 'button1', we will now finish the code to add 5 to the button's text property every 1 second, like so:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Interval = 1000
Button1.Text += 5
End Sub
This code can also be written as "Button1.Text = Button1.Text + 5" I hope this helps clear up the ambiguity.

Making a clock which would start running from specific time

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.

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