CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2001
    Location
    Rhode Island
    Posts
    56

    Cancel while in process

    I have a section of code that retrieves a number (variable amount) of records from a database. Upon retrieval, the code loops through the records one by one and performs a number of functions with these records. This can sometimes take too long for an impatient user and they want to cancel out of the operation. What is the best to allow the user to press a cancel button left on the screen? Usually, when I am executing code, there is no interaction by the user allowed... Any suggestions would be greatly appreciated!!

    Thank you.


  2. #2
    Join Date
    Jan 2000
    Location
    Saskatchewan, Canada
    Posts
    595

    Re: Cancel while in process

    Use of the keyword DoEvents in your loop will allow execution of your 'Cancel' button.

    David Paulson


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

    Re: Cancel while in process

    Small example of Davids suggestion. Start a new project. Add two command buttons. Paste this code into general declarations section. Run it. Click Command1 which starts a long loop.
    Click command2 to stop it.

    option Explicit
    '
    Dim blCancel as Boolean
    '
    private Sub Command1_Click()
    '
    LongLoopSub ' start a long loop
    MsgBox "Long Loop ended"
    '
    End Sub

    public Sub LongLoopSub()
    '
    Dim Something as string
    blCancel = false
    Do Until Something = "Hello there"
    '
    DoEvents
    If blCancel then Exit Sub
    '
    Loop
    '
    End Sub

    private Sub Command2_Click()
    '
    blCancel = true
    '
    End Sub




    John G

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