keeping time

Hello i am working on another project of mine and i need to have the date and time in my program. Certain things need to hapen like the caps lock turns on at 9:00 wich triggers a relay turning on the lights. So i just need to learn how to use time in my visual basic form. Thanks,

Average Joe



Answer this question

keeping time

  • SouthernPost

    You can use DateTime.Now to find out the current time, and set timers to go off when you want to do stuff.



  • Aafiq

    A simple example - use a timer control and in the tick event determine if the time is turn on or turn off time which will trigger you action.

    This will Check every minute. So its not acurate down to the second which you could reduce the tick interval time. At the moment its set to 60000 for 1 minute ticks.

    Public Class Form1
    Structure TimeSettings
    Dim hour As Integer
    Dim minute As Integer
    End Structure

    Dim startTime As New TimeSettings
    Dim endTime As New TimeSettings


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Timer1.Interval = 60000

    Me.Timer1.Enabled = True
    Me.Timer1.Start()

    startTime.hour = 19
    startTime.minute = 50

    endTime.hour = 19
    endTime.minute = 51
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Dim dt1 As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, 0)
    Dim dt2 As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, startTime.hour, startTime.minute, 0)
    Dim dt3 As DateTime = New DateTime(Now.Year, Now.Month, Now.Day, endTime.hour, endTime.minute, 0)

    If dt2 = dt1 Then
    Label1.text = "Process Start"
    ElseIf dt3 = dt1 Then
    Label1.text = "Process End"
    End If
    End Sub

    End Class


  • 5p4ced0ut

    The timer would be dropped on the form at design time.

    For something like this which was simply to show a use of the timer tick event I'd use it.

    You dont need to but I've had instances where it had been disabled the timer wouldnt work unless it is - therefore I enable it to make sure its enabled when I start the timer.

     

     


  • HerbertHerbert

    I don't see code here to construct the timer or associate it with the tick event Also, do you need to set enabled to true I've never done that.

    I guess you've added the timer in the design view and made the association there I never use the design view for stuff like that.



  • keeping time