Announcement

Collapse
No announcement yet.

using escape key to quit a for/next loop

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • using escape key to quit a for/next loop

    I remember seeing a post about getting out of a for/next loop with the escape key. My searches have not shown any thing. Any ideas would be much appreciated.

    John Tate

  • #2
    Try putting this inside the loop:

    IF GRAHIC INKEY$=CHR$(27) THEN JumpOutOfLoop:

    Comment


    • #3
      Or..
      Code:
      #DIM All
      #COMPILE EXE
      #INCLUDE "WIN32API.INC"
      
      FUNCTION PBMAIN()
       GetAsyncKeyState(%VK_ESCAPE)  ' Clear any previous notification
        Do
          Sleep 100
          If GetAsyncKeyState(%VK_ESCAPE) And &H0001 Then Exit loop
        Loop
      
        MSGBOX Time$,%MB_SystemModal,"Escaped!"
      
      END FUNCTION
      '------------------/PBMain
      Rgds, Dave

      Comment


      • #4
        For/Next specifically...


        FOR LoopCount& = 1 to 200000

        IF GRAPHIC$(INKEY$)=CHR$(27) THEN EXIT FOR

        NEXT LoopCount&

        Comment


        • #5
          Why are we assuming its a graphics application?

          What is wrong with con.inkey$?

          DO UNTIL CON.INKEY$$ = CHR$(27)
          do things
          LOOP
          [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
          Kerry Farmer

          Comment


          • #6
            I am using just a plain for/next loop, not a do/loop. I am not using any graphic commands. I am using PB windows 10.4.

            Comment


            • #7
              EXIT FOR works just as EXIT LOOP does.
              Rgds, Dave

              Comment


              • #8
                I remember seeing a post about getting out of a for/next loop with the escape key.
                AND
                I am using just a plain for/next loop, not a do/loop. I am not using any graphic commands. I am using PB windows 10.4
                If you are looking to exit a loop when <ESC> is pressed, then you HAVE to have something in your FOR/NEXT loop to look for the ESC key, since there is no real Windows' equivalent to the PB/DOS "ON KEY xx GOTO..." statement.

                With multiple threads of execution you have some options...

                GUI + Worker Thread + Abort Demo 11-24-07 (DDT syntax)

                Terminate Worker Threads Using Windows Events (and Using Thread Local Storage) Demo Dec 23 2005

                I think maybe the second one is what you want.

                If this is a GUI program and you are running this FOR/NEXT loop in the main GUI thread, I suggest you could use a bit of redesign.

                Code desiring 'exit loop on <ESC>' not shown.

                MCM
                Michael Mattias
                Tal Systems Inc.
                Racine WI USA
                mmattias@talsystems.com
                http://www.talsystems.com

                Comment


                • #9
                  Thanks Dave Biggs. I see what you mean now. Your suggestion works fine. I use the for/next loop with msgbox "show me something" when testing some data input.

                  John Tate

                  Comment


                  • #10

                    Anne, "Probably it is due to DIALOG END CBHNDL which cannot be used in Win 10", seems that win10 does work OK with DIALOG END CBHNDL .

                    This program is a large program. Three program in one. This program has the same code thru out the program where I have print preview function, and get errors .

                    This is where the program stop working, anywhere I use this print preview code.

                    CALLBACK FUNCTION Addr_Browse_Prt_Cancel() '<----- here seems to be the problem in this cancel callback.
                    ' ''DIALOG ENABLE hDlg3: ????
                    ' ''GRAPHIC DETACH: 'NUM USED IN PRT CURRENT

                    DIALOG ENABLE hDlg2:

                    ' ''CONTROL SET FOCUS hDlg2, 210

                    DIALOG END hDlg4, 1 '''
                    FUNCTION = 1
                    END FUNCTION

                    The above is where the program exits with error. Below is summary of what the sample does:

                    1. PBMAIN : here can start the program and exit the program with no errors. Here is where I have DIALOG END CBHNDL when I end program

                    2. From PBMAIN go to ADDRESS dialog . Can exit here back to PBMAIN and exit from there , NO errors .

                    3. From PBMAIN to ADDRESS and to BROWSE . Exit browse, exit address, exit pbmain no error.

                    4. From PBMAIN to ADDRESS to BROWSE and to BROWSE PRINT PREVIEW. Here when exit Browse print preview and get error 'program has stopped working"

                    I am opening pbmain then hiding to open address. Then end address dialog to go to browse. And disable browse to goto browse print preview. exit here error. Hope that explains the sample code a little better. Going to check links shown and Pierre's code now.

                    [i am editing because usually when posting a long reply, I am kicked out forum and have to log in again, but before I log in again I copy all my text reply. then when I log in again I paste my text which come all jumbled up with no spacing and have to fix it. hope one day you get more time to reply before being kick out of fourm. need fix ]
                    Last edited by Robert Alvarez; 14 Jul 2017, 04:46 PM.
                    Robert

                    Comment


                    • #11
                      Hi Robert,
                      Looks like you posted to the wrong Thread Need to cut n paste over to here

                      Hi John,

                      Using msgbox to monitor a program's progress can be disruptive (especially in a loop or callback procedure).
                      A better option can be to use DIALOG SET TEXT sMsg +" - "+ Time$ or WINBEEP to keep track.
                      The latest compilers also have TXT.PRINT to a TXT.WINDOW..
                      Rgds, Dave

                      Comment


                      • #12
                        Thanks again, Dave. I do not use the code to interrupt a for/next loop in the finished product. I usually use it to see if I have picked up all/correct items in an array, just when testing a new piece of a program.

                        John Tate

                        Comment

                        Working...
                        X