Cancel - a executing for next loop if a button is clicked

In my windows form based application I have a for each loop.  I want the application to do it stuff in the for next loop until it is done OR the user clicks on the cancel button.  How would i make it recognise that the cancel button has been clicked while the for loop buttons is executing in another procedure??
vbnetcoderAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You toggle a form level boolean in the cancel button handler.

In the loop, check the flag.  You must add DoEvents() inside the loop to allow the cancel button click to be processed.

*A more robust approach would be to place the work into a background thread with the BackgroundWorker() control.
0
 
vbnetcoderAuthor Commented:
It tells me that DoEvents is not declared
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry...for VB.Net you'd use "Application.DoEvents()".
0
 
vbnetcoderAuthor Commented:
That works thanks much!
0
 
vbnetcoderAuthor Commented:
ty
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.