Results 1 to 4 of 4

Thread: [RESOLVED] Countdown Timer on a different form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Resolved [RESOLVED] Countdown Timer on a different form

    Hello,

    From Form1, I'm trying to Call a sub on Form2 that displays a countdown timer in a label.
    First, I tried to it a different way by putting this code in Form1; above the sub "Dim tmrCD As Integer = 120" and code:

    Code:
    tmrCD = tmrCD - 1
                    If tmrCD > 0 Then frmTwo.lblCD.Text = ("NEXT ATTEMPT STARTS IN " & tmrCD.ToString)
                    If frmTwo.lblCD.Text = "0" Then
                        frmTwo.Timer1.Stop()
                    End If
    But it only displayed 119 and didn't count down. It was obvious to me that the reason is because the Timer is not being used.

    Then I tried putting this into Form2

    Code:
     Private CD As Integer = 120
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Me.lblCD.Text = ("NEXT ATTEMPT STARTS IN " & CD.ToString("00"))
            If CD = 0 Then
                Me.Timer1.Enabled = False
            Else
                CD -= 1
            End If
        End Sub
    But it started counting down the instant the page loaded, which is completely opposite from what I need! I need Form1 to finish it's job when a button is pressed, THEN call Timer1_Tick at the bottom of the sub on Form1 when it's finished with it's job. In form1, I tried using:

    Code:
    Call frmTwo.Timer1
    But got the error:

    Property access must assign to the property or use it's value.
    I'm not sure how to proceed. Ideas?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Countdown Timer on a different form

    Add public sub in frmTwo and call it from form1, frmTwo.StartCountDownTimer

    Code:
    ' add this to frmTwo
    Public Sub StartCountDownTimer()
        Timer1.Interval = 1000
        Timer1.Start
    End Sub



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Posts
    123

    Re: Countdown Timer on a different form

    Worked perfectly!

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: [RESOLVED] Countdown Timer on a different form

    BTW: you can start the timer like this frmTwo.Timer1.Start(), but i suggest using public sub because it is more handy specially if you want to perform some tasks before or after starting the timer.



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