CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2001
    Posts
    2

    Exiting from loops

    I'm a complete beginner to VB programming, and I've enconutered this problem for which I need the solution urgently. A critical part of my application involves a lengthy for-next loop. I want a method by which the user can click on a command button and exit the loop at any time during which the the loop is running. Right now what happens is, when the user clicks a command button during the running of the loop, the loop continues to run, and only when it finishes does the subroutine associated with the command button take place.

    Thanx in advance for any help received

    Abhik Majumdar


  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Exiting from loops

    You need to add the DOEVENTS statement sporadically in your loop. This allows other events to take place outside your loop. THe outside procedure could set a switch telling the loop to abort if set.
    A small example

    option Explicit
    Dim blAbort as Boolean
    private Sub cmdStartLoop_Click()
    Dim x
    Do Until x = 1000
    DoEvents
    If blAbort then Exit Do
    Loop
    Unload me
    End Sub

    private Sub cmdStopLoop_Click()
    blAbort = true
    End Sub




    John G

  3. #3
    Join Date
    Aug 2001
    Posts
    14

    Re: Exiting from loops

    It might be helpful to change it to a Do...Loop, if you know how, and then the Sentinel would be easy to set, plus a global variable could easily be checked every so often (at the beginning, middle and end) during the loop to see if you want to break it... in which case, you could execute the sentinel.

    For...Loops are much better in C++.


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

Featured