Results 1 to 9 of 9

Thread: Stopping loop with command button

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    3

    Question Stopping loop with command button

    I want to do a
    Do Until Loop
    stopping when a command button is clicked.

  2. #2
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    Loop while

    Code:
    whoops: you asked for a do until...
    well, easy:
    'on top of form
    dim blnStop as boolean
    '--------------------------------
    'where you need the loop
    Do Until blnStop = True
         DoEvents
    Loop
    blnStop= False
    '--------------------------------
    
    'on click of that button:
    Private Sub Command1_Click()
         blnStop = true
    End Sub
    '*********************
    'Previous code suggested used "Do While"
    'on top of form
    dim blnStop as boolean
    '--------------------------------
    'where you need the loop
    do while blnStop = false
         doevents
    loop
    blnStop= False
    '--------------------------------
    
    'on click of that button:
    Private Sub Command1_Click()
         blnStop = true
    End Sub
    '--------------------------------
    Special thanks to some wonderful people,
    such as Lothar the Great Haensler, Aaron Young,
    dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....

  3. #3
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    a loop cannot be stoped directly.... u can create threads and that is possible in .net so in vb6 and below u need to add a check..

    in the loop add a check (a varible)
    on button clcik change the value of the check variable

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    3
    have tried this but unable to get out of loop!

  5. #5
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    Originally posted by tottnik
    have tried this but unable to get out of loop!
    sorry... i think that can be done in diff event

    as i said that is possible in .net vb is a single thread so untill ur loop completes nothing can be done... i am not sure try using a timer and change the value of the check variable

  6. #6
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Belgium/Antwerp
    Posts
    275
    Made a little app, and this seems to work fine.

    Place this behind a form with 2 buttons and a label:

    Option Explicit

    Dim b As Boolean

    Private Sub cmdStop_Click()
    b = False
    End Sub

    Private Sub Command1_Click()

    b = True
    Dim i As Long
    Do
    i = i + 1
    Me.Label1.Caption = i
    DoEvents
    Loop Until b = False
    End Sub

    Greetz,

    Luc

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    DoEvents is the way to get this done. It will pause execution to process other events like button presses.

  8. #8
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    You should have some kind of a delay in your loop to prevent draining the PC's resources...

    J.A.T.
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    3
    Thanks everyone, I have tried your code Luc and it works though there seems some delay between clicking cmdStop and the loop stopping.
    Shaggy i really wanted to stop execution of loop rather than just pause it.
    Cheers everyone

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