Results 1 to 6 of 6

Thread: [RESOLVED] Perform action only when the time exactly match

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Resolved [RESOLVED] Perform action only when the time exactly match

    Stupid question, but...
    I want to show my second form only when time will be exactly 08:00:00 and again at 15:00:00.
    Kinda works - it shown msgbox when time does not match, but if it match, form2 constantly opens when I close it, althought it should open only once and immediately should open msgboxes every one seconds, because time does not match.
    Second, I set the interval to 0.5 sec to check time (because there can be mismatch!), but the msgbox still opens every 1 second. Of course it´s also altered inside timer in properties.

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer1.Interval = 500
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = Date.Now.ToString("HH")
            Label2.Text = Date.Now.ToString("mm")
            Label3.Text = Date.Now.ToString("ss")
            If Label1.Text = "08" And Label2.Text = "00" And Label3.Text = "00" Then
                Form2.Show()
                Timer1.Stop()
                Timer1.Start()
            Else
                MsgBox("Time does not match, sorry!")
            End If
            If Label1.Text = "15" And Label2.Text = "00" And Label3.Text = "00" Then
                Form2.Show()
            Else
            End If
        End Sub

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Perform action only when the time exactly match

    hi

    try this way
    add a Label to Form1
    and I just opened Form2 when the set Time was reached

    Code:
    Option Strict On
    
    Public Class Form1
    
        Dim WithEvents timer1 As New Timer With {.Enabled = True, .Interval = 1000}
        ' alarmtime 
        Dim alarmTime As DateTime = DateTime.Parse("19:08:00")
    
       
    
    
        Private Sub timér1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
            ' show time
            Label1.Text = DateTime.Now.ToString("HHH:mm:ss")
            ' compare the time
            If DateTime.Now > alarmTime Then
                ' time's up do somthing
                Form2.Show()
                timer1.Stop()
            End If
        End Sub
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Perform action only when the time exactly match

    Code:
    Option Strict On
    Public Class Form1
        Dim WithEvents timer2 As New Timer With {.Enabled = True, .Interval = 1000}
        ' alarmtime 
        Dim alarmTime As DateTime = DateTime.Parse("08:00:00")
        Private Sub timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
            ' show time
            Label1.Text = DateTime.Now.ToString("HHH:mm:ss")
            ' compare the time
            If DateTime.Now > alarmTime Then
                ' time's up do somthing
                Form2.Show()
                timer2.Stop()
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            timer2.Start()
        End Sub
    End Class
    it does not work... Form2 isn´t displayed and Label1.Text is Label1.
    Last edited by VB.NET Developer; Oct 23rd, 2019 at 12:40 PM.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Perform action only when the time exactly match

    you missed something

    see Blue
    Code:
     Private Sub timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer2.Tick
    you also don't have to start the Timer in Form_Load
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: Perform action only when the time exactly match

    this works for me:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Timer2.Enabled = True
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = Date.Now.ToString("HH:mm:ss")
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Label1.Text = "08:00:00" Or Label1.Text = "15:00:00" Then
                Form2.Show()
            Else
            End If
        End Sub
    Solved!
    Last edited by VB.NET Developer; Oct 23rd, 2019 at 01:19 PM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Perform action only when the time exactly match

    Instead of having the Timer Tick every second and comparing the current time, why not just have the Timer Tick at the time you want? Just calculate the time between now and the next Tick and set that as the Interval. A Timer does drift a bit but not so much in that short period that it should matter. If you want it more accurate then set the Interval so that it Ticks a minute before and then set the Interval to whatever time remains. The drift over that last minute will be negligible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width