The information in this article applies to:
- Microsoft FoxPro for Windows, versions 2.5x, 2.6, 2.6a
SUMMARY
This article explains how to cancel a process that has been started with a
READ command. This is functionally similar to being able to press the
Cancel button to terminate a print job in any Windows application.
MORE INFORMATION
A separate Cancel button whose VALID clause contains a CLEAR READ or some
other terminating code will not work, since FoxPro cannot initiate the
Cancel button while a previous READ is still in effect. The sample code
below shows how to work around this limitation.
Steps to Create a Cancel Button for an Active READ
- Create a Quick Screen using one of the smaller .DBF files in the FoxPro
TUTORIAL directory, such as the MOREREG.DBF database file. This file
ships with approximately 11 records.
- Near the center of the screen, create a button called Process, and
assign this button a variable named "scrlrecs" (without the quotation
marks). (SCRLRECS stands for "scroll records.") In the VALID clause for
Process, enter the following code:
SHOW GET scrlrecs DISABLED
m.cancel = .F.
DO WHILE m.cancel = .F.
SHOW GETS LEVEL 1
SKIP
IF EOF( )
GO TOP
ENDIF
@ 8.000,52.000 GET cnclbttn PICTURE "@*HN Cancel" ;
SIZE 1.625,8.125,0.500 DEFAULT 1 FONT "MS Sans Serif",8 ;
STYLE "B" VALID gocancel( )
READ CYCLE TIMEOUT .25
ENDDO
GO TOP
SHOW GETS
SHOW GET scrlrecs ENABLED
PROCEDURE gocancel
m.cancel = .T.
WAIT WINDOW 'Process Terminating' TIMEOUT 2
RETURN
Below the Process button, create another button called Exit and
assign the Exit button a variable named "extscx" (without the quotation
marks). (EXTSCX stands for "exit screen.")
Select the Terminate READ On Selection check box.
Save the screen as MYCANCEL.SCX and generate a MYCANCEL.SPR file.
Explanation of Procedures
- After MYCANCEL.SPR has been run, and Process button chosen, the VALID
for this button disables the Process button.
- The VALID clause creates a memory variable called M.CANCEL and assigns
it the logical value of false (.F.).
- The DO WHILE loop begins running.
- As long as M.CANCEL maintains its value of false, the DO WHILE loop
cycles repeatedly, executing the SHOW GETS, which display the values
of the fields for the current record.
- SKIP controls the advancement of the database record pointer.
- The IF statement is executed within the DO WHILE loop to determine if
the record pointer has moved beyond the last record in the database. If
so, the pointer is moved back to the first record of the database.
- Each pass through the DO WHILE loop redefines an @ ... GET which is
assigned the variable name CNCLBTTN (cancel button), and displays a
picture of a button named Cancel.
- A VALID is assigned to this @ ... GET statement, which, when executed,
runs the GOCANCEL procedure.
- The READ for this @ ... Get statement is timed out after 1/4 of a
second if the VALID is not executed.
- Choosing the Cancel button suspends the DO WHILE loop and runs the
GOCANCEL procedure. The GOCANCEL procedure does the following:
a. Changes the value of M.CANCEL to true.
b. Places a WAIT WINDOW on the desktop for two seconds.
c. Performs a RETURN to the DO WHILE loop.
- Because M.CANCEL is now true, the DO WHILE loop ends.
- The remaining code moves the record pointer to the first record in the
database, displays the field values of record #1, and reenables the
Process button.
|