Results 1 to 17 of 17

Thread: Timer As A Loop Ender?!?!?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Resolved Timer As A Loop Ender?!?!?

    Hey,

    For Ill tell you what this code is trying to do...
    It counts to as much as it can, as fast as it can all adding up to n the variable. But when the Timer's Interval = 10000 it should end.
    Any way to do this? btw... I will make the timer's action do something else later, just for now i wrote that
    Thanks I'm not very good at loops, my loops keep making my vb crash
    gl!

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     Let n = 1
    4. Do Until Interval = 10000    
    5.     Picture1.Cls
    6.     Picture1.Print n + 1
    7. loop
    8. End Sub
    9.  
    10.  
    11. Private Sub Timer1_Timer()
    12.     Unload Form1
    13. End Sub
    Last edited by Brin; Apr 18th, 2005 at 06:23 PM.

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Timer As A Loop Ender?!?!?

    Hmm... Interesting idea.

    Try this:

    VB Code:
    1. Dim lngCurrentInterval As Long
    2.  
    3. Private Sub Form_Load()
    4.     lngCurrentInterval = 1
    5. End Sub
    6.  
    7. Private Sub Command1_Click()
    8.     Dim n As String
    9.     Let n = 1
    10.     Do Until lngCurrentInterval = 10000    
    11.         Picture1.Cls
    12.         Picture1.Print n + 1
    13.     Loop
    14. End Sub
    15.  
    16. Private Sub Timer1_Timer()
    17.     lngCurrentInterval = lngCurrentInterval + 1
    18. End Sub


    Not shure if that is whathats all I can think of at this moment

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  3. #3
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Timer As A Loop Ender?!?!?

    Quote Originally Posted by Brin
    Hey,

    For Ill tell you what this code is trying to do...
    It counts to as much as it can, as fast as it can all adding up to n the variable. But when the Timer's Interval = 10000 it should end.
    Any way to do this? btw... I will make the timer's action do something else later, just for now i wrote that
    Thanks I'm not very good at loops, my loops keep making my vb crash
    gl!

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     Let n = 1
    4. Do Until Interval = 10000    
    5.     Picture1.Cls
    6.     Picture1.Print n + 1
    7. loop
    8. End Sub
    9.  
    10.  
    11. Private Sub Timer1_Timer()
    12.     Unload Form1
    13. End Sub
    Your code would never end, unless you exit the do loop. Besides, Interval is a (not-defined) variable. If you want to ask for the timer's property you should say so: Timer1.Interval. Anyway... I don't see you're changing this value, so... if the Interval is not 10000 your loop will simply never end.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Timer As A Loop Ender?!?!?

    Quote Originally Posted by sciguyryan
    Hmm... Interesting idea.

    Try this:

    VB Code:
    1. Dim lngCurrentInterval As Long
    2.  
    3. Private Sub Form_Load()
    4.     lngCurrentInterval = 1
    5. End Sub
    6.  
    7. Private Sub Command1_Click()
    8.     Dim n As String
    9.     Let n = 1
    10.     Do Until lngCurrentInterval = 10000    
    11.         Picture1.Cls
    12.         Picture1.Print n + 1
    13.     Loop
    14. End Sub
    15.  
    16. Private Sub Timer1_Timer()
    17.     lngCurrentInterval = lngCurrentInterval + 1
    18. End Sub


    Not shure if that is whathats all I can think of at this moment

    Cheers,

    RyanJ

    This prints '2's for about 25 seconds, then crashes

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Re: Timer As A Loop Ender?!?!?

    Ehh Yup...
    I got another program... I have my vb 5.0 book right over here, the problem is im using vb 6. Still it should work since 5.0 is older then 6.0 right?

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     Let n = 1
    4.     Let Timer1_Timer.Enabled = True
    5.  
    6. Do Until Timer1.Interval = 10000
    7.  
    8.    
    9.     Picture1.Print n + 1
    10. Loop
    11. End Sub
    12.  
    13.  
    14. Private Sub Timer1_Timer()
    15.    
    16.    Load Form2
    17.    Form2.Show
    18. Picture2.Print "Congrats!  Your computer counted to "; n; " In 10 seconds!"
    19.    Unload Form1
    20.    
    21. End Sub

    My vb doesn't recognise this
    VB Code:
    1. Let Timer1_Timer.Enabled = True
    Higlights Timer1_Timer And Says
    "Expected fuction or variable."
    Hmm?!?!?!

  6. #6
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Timer As A Loop Ender?!?!?

    The Let keyword is no longer needed - it was mostly used in the older versions of the language

    Try:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     n = 1
    4.     Timer1_Timer.Enabled = True
    5. Do Until Timer1.Interval = 10000
    6.     Picture1.Print n + 1
    7. Loop
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.    Load Form2
    12.    Form2.Show
    13. Picture2.Print "Congrats!  Your computer counted to "; n; " In 10 seconds!"
    14.    Unload Form1
    15. End Sub


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Re: Timer As A Loop Ender?!?!?

    Hmm.... tried it but it still gives me the same error?

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Timer As A Loop Ender?!?!?

    what is setting the interval, and when is it changing?

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Timer As A Loop Ender?!?!?

    No wonder why it cannot recognize "Timer1_Timer.Enabled". The object's name is Timer1, not Timer1_Timer. Besides... you're not understanding how a timer works. The Interval's property is explained as:

    Returns/sets the number of milliseconds between calls to a Timer control's Timer event.
    You won't get the Interval property incremented each second it passes. Re-write your code. That won't work.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Timer As A Loop Ender?!?!?

    Something like this would be better.

    VB Code:
    1. Private Sub Command1_Click()
    2.   Timer1.Interval = 1000
    3.   dim x as integer, n as integer
    4.   x=0
    5.   timer1.enabled = true
    6.   do while x<11
    7.     debug.print n
    8.     n=n+1
    9.   loop
    10. end sub
    11.  
    12. private sub timer1.timer
    13.   x=x+1
    14. end sub

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Re: Timer As A Loop Ender?!?!?

    Hey dglienna, just saw your post while posting.. I got another code it works with the timer, not just with n + 1 and so on....

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     n = 1
    4.     Timer1.Enabled = True
    5. Do Until Timer1.Interval = 10000
    6.     'Picture2.Print "Congrats!  Your computer counted to "; n + 1; " In 10 seconds!"
    7. Loop
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.  
    12. Picture2.Print "Congrats!  Your computer counted to "; n; " In 10 seconds!"
    13. Timer1.Enabled = False
    14. End Sub

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Timer As A Loop Ender?!?!?

    That WON'T WORK. for the same reasons we've been saying.
    Look at my code. That is how to use the timer. The way you are trying will not END. EVER!

  13. #13
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Timer As A Loop Ender?!?!?

    Quote Originally Posted by Brin
    Hey dglienna, just saw your post while posting.. I got another code it works with the timer, not just with n + 1 and so on....

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim n As String
    3.     n = 1
    4.     Timer1.Enabled = True
    5. Do Until Timer1.Interval = 10000
    6.     'Picture2.Print "Congrats!  Your computer counted to "; n + 1; " In 10 seconds!"
    7. Loop
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.  
    12. Picture2.Print "Congrats!  Your computer counted to "; n; " In 10 seconds!"
    13. Timer1.Enabled = False
    14. End Sub
    Brin, did you read any of my posts? There are several things wrong. Do you understand what I state in each post. If you don't, ask. But, what you're doing is still wrong.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Re: Timer As A Loop Ender?!?!?

    Ok, I tried what you said...
    VB Code:
    1. Private Sub Command1_Click()
    2.   Timer1.Interval = 1000
    3.   Dim x As Integer, n As Integer
    4.   x = 0
    5.   Timer1.Enabled = True
    6.   Do While x < 11
    7.     Debug.Print n
    8.     n = n + 1
    9.   Loop
    10. End Sub
    11.  
    12. Private Sub timer1_timer()
    13.   x = x + 1
    14. End Sub
    btw... vb 6.0 doesnt accept private sub names which have . in them... And after each name there has to be a () either with something in it or left blank...

    So when I do the upper code I get an overflow error.
    I see that the code that you wrote relies on the error to happen hence
    VB Code:
    1. Debug.Print n
    but rather then printing the variable n it prints Overflow error

  15. #15
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Timer As A Loop Ender?!?!?

    The code needed several corerctions.
    VB Code:
    1. Option Explicit
    2.  
    3. Private x As Integer
    4.  
    5. Private Sub Command1_Click()
    6.   Timer1.Interval = 1000
    7.   Dim n As Long
    8.   x = 0
    9.   Timer1.Enabled = True
    10.   Do While x < 11
    11.     Debug.Print n
    12.     n = n + 1
    13.     DoEvents
    14.   Loop
    15.  
    16.   Timer1.Enabled = False
    17. End Sub
    18.  
    19. Private Sub Timer1_Timer()
    20.   x = x + 1
    21. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  16. #16
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Timer As A Loop Ender?!?!?

    Sorry, I thought you were just looking at the method. I wrote it in the editor.
    My mistake.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Apr 2005
    Posts
    127

    Re: Timer As A Loop Ender?!?!?

    Yup...Works like a charm
    And I looked at the back of me book, "DoEvents", nice command, learned something new

    btw... I added a picture2.print so it would work too if an error didnt occur, which doesn't in mine, the first time i tried...

    Again, Thanks! All Ill rate yer posts in an hour, gotta go somewhere
    bbs(BeBackSoon)

    VB Code:
    1. Option Explicit
    2.  
    3. Private x As Integer
    4.  
    5. Private Sub Command1_Click()
    6.   Timer1.Interval = 1000
    7.   Dim n As Long
    8.   x = 0
    9.   Timer1.Enabled = True
    10.   Do While x < 11
    11.     Debug.Print n
    12.     n = n + 1
    13.     DoEvents
    14.   Loop
    15.   Picture2.Print "Congrats!  Your computer counted to "; n; " In 10 seconds!"
    16.   Timer1.Enabled = False
    17. End Sub
    18.  
    19. Private Sub Timer1_Timer()
    20.   x = x + 1
    21. End Sub





    AGain, THANKS!!!!

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