|
-
Oct 15th, 2002, 08:57 AM
#1
Thread Starter
New Member
Stopping loop with command button
I want to do a
Do Until Loop
stopping when a command button is clicked.
-
Oct 15th, 2002, 09:02 AM
#2
Addicted Member
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....
-
Oct 15th, 2002, 09:07 AM
#3
Fanatic Member
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
-
Oct 15th, 2002, 09:12 AM
#4
Thread Starter
New Member
have tried this but unable to get out of loop!
-
Oct 15th, 2002, 09:15 AM
#5
Fanatic Member
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
-
Oct 15th, 2002, 09:24 AM
#6
Hyperactive Member
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
-
Oct 15th, 2002, 12:49 PM
#7
DoEvents is the way to get this done. It will pause execution to process other events like button presses.
-
Oct 15th, 2002, 12:51 PM
#8
Frenzied Member
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".
-
Oct 16th, 2002, 10:27 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|