Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I enable cancellation in a loop

Forms

How can I enable cancellation in a loop

by  ChrisRChamberlain  Posted    (Edited  )
If you need a simple way to enable a user to cancel out of loops, in particular where external processing is involved, try the following, (VFP 7 and above).

If an external process is running, it is generally easier to allow the external process to complete, resolve any issues arising and then exit the loop.

Add a command button to a form and name it [color blue].cmdCancel[/color], with [color blue].Enabled = .F.[/color]
Add two properties to the form, [color blue].lQueryExit[/color] and [color blue].lCancelButton[/color].

In the [color blue].MouseEnter()[/color] event of [color blue].cmdCancel[/color] put :-

[color blue]THISFORM.lCancelButton = .T.[/color]

In the [color blue].MouseLeave()[/color] event of [color blue].cmdCancel[/color] put :-

[color blue]THISFORM.lCancelButton = .F.[/color]

In the following code, the command button is enabled for a couple of seconds during which time, should a user click on [color blue].cmdCancel[/color], the form property [color blue].lQueryExit[/color] becomes true enabling the relevant [color blue]MESSAGEBOX[/color] dialog to take place.

The form property [color blue].lCancelButton[/color] only becomes true if the mouse click was detected whilst the mouse was over the command button [color blue].cmdCancel[/color].

There is no requirement for code in the command button as the [color blue].Click()[/color] event was not fired but as far as the user is concerned it appears to have happened.

For simplicity, all the required code remains within the loop and in the example below, the user can skip a record, ignore and continue, or cancel the entire operation.

[color blue]
LOCAL inval1, lnSeconds, lnAnswer

WITH THISFORM

SELECT TABLENAME

SCAN

DOEVENTS
[/color][color green]
&& Allow user to cancel[/color][color blue]
WITH .cmdCancel
.Enabled = .T.
.Refresh()
ENDW

INKEY(0.001)

lnSeconds = SECONDS()
DO WHILE .T.
inval1 = INKEY(0.06,'HM')
IF SECONDS() > lnSeconds + 2.5
EXIT
ENDI
IF inval1 = 151 AND .lCancelButton
.lQueryExit = .T.
EXIT
ENDIF
ENDD

WITH .cmdCancel
.Enabled = .F.
.Refresh()
ENDW

INKEY(0.001)

IF .lQueryExit
lnAnswer = MESSAGEBOX( ;
[Do you want to skip the deletion of ] ;
+ ALLT(TABLENAME.filename) ;
+ [?] ;
+ CHR(13) ;
+ CHR(13) ;
+ [If you answer 'Cancel', you will cancel the entire operation] ,;
3 + 32 + 256 ,;
[Skip file deletion or cancel operation])
DO CASE
CASE lnAnswer = 2
EXIT
CASE lnAnswer = 6
.lQueryExit = .F.
LOOP
OTHERWISE
.lQueryExit = .F.
ENDC
ENDI
[/color][color green]
&& Processing, internal and/or external, here[/color][color blue]
ENDS

IF .lQueryExit
TABLEREVERT(.T.,[TABLENAME])
ELSE
TABLEUPDATE(.T.,.T.,[TABLENAME])
ENDI
ENDW[/color]
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top