CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Timer Question

  1. #1
    Guest

    Timer Question

    Anyone know how to stop a timer?

    Have tried Timer1.Enabled = False as it states at msdn.microsoft.com but this does NOT stop the timer.

    Any help for this beginner would be appreciated.


  2. #2

    Re: Timer Question

    Timer1.enabled = false should work. Why don't you post your code so we can see what you are doing?

    Charlie Zimmerman
    http://www.freevbcode.com
    [email protected]


  3. #3
    Guest

    Re: Timer Question

    OK, here's the code:

    Private Sub Form_Load()
    'Center the Splash form on screen
    frmSplash.Top = (Screen.Height - frmSplash.Height) / 2
    frmSplash.Left = (Screen.Width - frmSplash.Width) / 2
    'Show Splash Form
    frmSplash.Show vbModeless
    'Make sure Splash window stays on top
    'grSetAsTopMost frmSplash, True
    'Show progress bar status
    Dim StartTime As Integer
    StartTime = Second(Now)
    prgBar.Visible = True
    While time_elapsed < Timer1.Interval / 1000 And _
    Timer1.Enabled = True
    time_elapsed = (Second(Now) - StartTime)
    prgBar.Value = time_elapsed
    DoEvents
    Wend
    End Sub


    Private Sub Frame1_Click()
    'Unload Splash form if the user clicks on it
    Timer1_Timer
    End Sub


    Private Sub Timer1_Timer()
    'Unload Splash form when timer has expired (approx. 5 seconds)
    Timer1.Enabled = False
    Unload frmSplash
    grSetAsTopMost frmMDIForm, True
    frmMDIForm.Show
    End Sub



  4. #4

    Re: Timer Question

    Your code has two errors:

    1) Because time_elapsed as not been declared, it is an empty variant. You should either put Dim time_elapsed as integer and/or time_elapsed = 0 into your code (recommendation: put both).

    2) The first line of the While Loop should read

    While time_elapsed < Timer1.Interval / 1000 And _
    Timer1.Enabled = true




    Also, what you are trying to do can be accomplished more simply by using the Sleep API function. See http://www.freevbcode.com/ShowCode.Asp?ID=74 for an example.


  5. #5
    Guest

    Re: Timer Question

    time_elapsed WAS declared in (General) as it is used elsewhere in my code.

    the while loop DOES read Timer1.Enabled = True


  6. #6

    Re: Timer Question

    Then I don't know what to tell you. I got the following code to work just fine (it is the same logic with some of the other stuff commented out:

    private Sub Form_Load()
    'Center the Splash form on screen
    Form2.Top = (Screen.Height - Form2.Height) / 2
    Form2.Left = (Screen.Width - Form2.Width) / 2
    'Show Splash Form
    Form2.Show vbModeless
    'Make sure Splash window stays on top
    'grSetAsTopMost Form2, true
    'Show progress bar status
    Dim StartTime as Integer
    StartTime = Second(Now)
    time_elapsed = 0
    ' prgBar.Visible = true
    While time_elapsed < Timer1.Interval / 1000 And _
    Timer1.Enabled = true
    time_elapsed = (Second(Now) - StartTime)
    'prgBar.Value = time_elapsed
    DoEvents
    Wend
    End Sub

    private Sub Timer1_Timer()
    'Unload Splash form when timer has expired (approx. 5 seconds)
    Timer1.Enabled = false
    Unload Form2
    ' grSetAsTopMost frmMDIForm, true
    'frmMDIForm.Show
    End Sub






  7. #7
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Timer Question

    What do you mean "does NOT stop the timer"? When you unload the form, the timer is destroyed. What does it do after it's supposed to have been stopped, but doesn't?


  8. #8
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Timer Question

    Hi,
    1.
    What confuses me is Which Form's form_load is this code? czimmerman seems to think that this is the form load of a form which shows a different Splash form in its form load (which is what i too thought in the first look at the code) then what confused me was these lines:
    grSetAsTopMost frmMDIForm, True
    frmMDIForm.Show
    When is this form (ie 'Me') is unloaded?
    2. Dont put a while-wend loop in the Form_Load.
    Form load is not finished till your timer is done, which could give trouble with DoEvents which will allow other events to be processed.
    Do a simple modification to your code:
    In Decl section:
    dim timeofstart
    private Sub Form_Load()
    ... other code
    frmsplash.show VbModeless
    ....
    timeofstart = timer() ' Timer() is another VB fn meant for this kind of stuff
    timer1.interval = 500 ' Every .5 sec
    timer1.Enabled = true
    end sub
    ' Modify your timer event like this:
    private Sub Timer1_Timer()
    if timer() >= timeofstart + 5 then' after 5 secs stop
    'Unload Splash form
    Timer1.Enabled = false
    Unload frmSplash
    grSetAsTopMost frmMDIForm, true
    frmMDIForm.Show
    else
    prgBar.Value = timer() - timeofstart
    end if
    End Sub




    RK

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

Featured