Results 1 to 14 of 14

Thread: Timers seem like a WASTE of time...

  1. #1

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Thumbs down

    Let's say I wanted to display the numbers between 1 and 1000. In qbasic I would do the following:

    ---------------
    For X = 1 to 1000
    Print X
    Next X
    ---------------

    It continually updates and prints X.
    In vb, if I want basically the same thing, I type this:

    -------------
    For X = 1 to 1000
    Label1.Caption = X
    Next X
    -------------

    Problem here is that it doesn't print 1-2-3 etc, it just goes until it's finished and prints 1000.

    Solution? Timers. Or are they?

    To create the effect I want I have always had to do this:

    ------------
    In the General Declarations:
    Dim X as Integer

    In a command button:
    X = 0
    Timer1.Enabled = True

    In timer1:
    X = X + 1
    Label1.Caption = X
    If X = 1000 Then Timer1.Enabled = False
    -------------

    This seems extremely inefficient, and those timer intervals appear to be sort of inconsistent. There's got to be an alternative, what is it?
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    try this:
    Code:
    for x = 1 to 100
    label1.caption = label1.caption & x
    next x

  3. #3

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Unhappy

    Uhh...how should I put this? No. That makes the caption = "123456789101112..." and I just want it to go "1", "2", "3", etc.
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  4. #4
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    is this what you want:

    Code:
    for x = 1 to 100
    label1.caption = label1.caption & x & ", "
    next x
    NXSupport - Your one-stop source for computer help

  5. #5

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163
    Oyi! I must be really bad at explaining this. First of all, I'm not really trying to make this X loop, it was just an example I could use if I had an alternative to the timer junk.

    AS for the X loop example- you know how a digital timer that counts up would first display 1, then it would erase that one and display a 2, then it would erase that two and display a 3??? It's that simple. I was just trying to find an alternative way of doing that, because it seems like the timer is not the best way.
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  6. #6
    Guest
    Do you mean display the number for a second and then erase it and display a new number?

    Code:
    Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)
    
    Private Sub Command1_Click()
    For x = 0 To 100
    Me.Caption = x
    Call Sleep(1000)
    DoEvents
    Next x
    End Sub

  7. #7
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Cleveland, Ohio
    Posts
    263
    Try:

    Code:
    For x = 0 To 100
    Me.Caption = x
    DoEvents 'duration of a millisecond.
    Next x
    or:
    Code:
    For x = 0 To 100
    Me.Caption = x
    Me.Refresh 'repaints the form.
    Next x
    However, both of these examples will work extremely quickly, so if you're aiming to use a more timed interval, Matthew Gates solution will work well.

  8. #8

    Thread Starter
    Addicted Member krah's Avatar
    Join Date
    Jan 1999
    Location
    Arkansas, her hyuck!
    Posts
    163

    Thumbs up

    Well Phobic has done it. I don't understand the DoEvents in the first example, but the second example was what I was looking for. It works much faster than a timer set to intervals of 1 ms. Thanks, Phobic!
    Is it tired in here or is it just me?

    Ryan Williams
    -Using Vb6-

  9. #9
    Guest
    Use DoEvents()!

    DoEvents tell windows to go and do what it needs to do, stuff like update the screen, write stuffs to disk that has been waiting for a while.

    Basically it transfers control to windows and waits until it returns control to your program when it's done.

    I use doevents in tight loops, to keep my programs 'user friendly' during long calculations, that way a user will always be able to click cancel or close the program AND the calculation will be performed at the fastest possible speed instead of at intervals when using a timer.

    Gerco Dries.

  10. #10
    Guest
    The simplest solution (as always) would be this:

    Code:
    for x=1 to 1000
      label1.caption=x
      label1.refresh
    next x
    which saves a DoEvents

  11. #11
    Guest
    Yes, but IMHO DoEvents is more user/system friendly.

    Have you ever tried this:

    Code:
    do
      x=x+1
      if x=10000 then x=0
    loop
    Your system will lock up unless you insert a DoEvents in there and you won't even be able to close your program with the taskmanager (unless ur using Windows NT).

    a label1.refresh only works for GUI elements, not for expensive calculations.

    Gerco Dries.


    [Edited by Gerco on 09-18-2000 at 02:02 AM]

  12. #12
    Guest

    Talking P.S:

    You can also use a timer, but you said that intervals seems like inconsistent.
    all right!
    just try to modify timer.interval, that is the number of milliseconds between each activation of the timer.
    In fact, tri tu use timer.interval= 200
    that wold work with no problem,and it's easy, without writing any loop.
    Regards
    Gvu

  13. #13
    Guest
    You example of using Qbasic will output the results on the screen as follows:

    1
    2
    3
    4
    5
    ...
    998
    999
    1000

    To do this in VB, you can use the Print method (just like QB)
    Code:
    For I = 1 To 1000
    Print I
    Next I

  14. #14
    Guest
    You could also specify the object, like a picturebox:
    Code:
    For x = 1 to 1000
        OBJECT.print x
    Next

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