Step 6: Browsing Through a Stack and Using Triggers

In this section:

Goal

In the first stage of this application, you used a Winform that displays the CustInfo stack. However, this Winform displays only one row of the stack and does not offer a way of moving through the stack to display different rows.

Now you enhance the application so the clerk can browse through the stack by using triggers. A trigger is logic that is invoked ("triggered") by a specified event. Each time that the event occurs, the logic is invoked. In Maintain, the invoked logic is a function or a system action, and the event is something the user does in a Winform. (Triggers are also known as event handlers.)


Top of page

x
Methods: Winform Painter, Triggers, IF, FocIndex, FocCount

You can use the Winform Painter to generate a stack browser in your Winform, as described in Using the Winform Painter. The Winform facility manages the browser automatically without inserting any code into the Maintain procedure.

To illustrate the principles of stack manipulation for this sample application, you code your own browser. In addition to making the browser mechanism visible, this also enables you to enhance the basic browser in Step 7: Displaying and Editing an Entire Stack in a Winform.

Your browser uses the IF command, together with the FocIndex and FocCount stack variables, to loop through the stack. You put this logic into a function and then assign the function to a function-key trigger. Whenever a user presses a specified function key, the trigger invokes the function, and the logic in the function moves through the stack one row at a time.

The basic logic, shown here for the CustInfo stack, is:

IF CustInfo.FocIndex LT CustInfo.FocCount
THEN COMPUTE CustInfo.FocIndex = CustInfo.FocIndex + 1;

The IF command tests whether the current position in the stack (FocIndex) has reached the last record in the stack (FocCount). If it has not, then it can continue to move forward through the stack one row at a time, so it increases the current position by one. When control leaves the function and returns to the Winform, the next row is displayed.

If, when browsing through the stack, you wish to return to the first row after displaying the last row, you can add the following ELSE phrase to the generated IF command:

ELSE COMPUTE CustInfo.FocIndex = 1;

Top of page

x
Solution

You implement the stack browser by coding the following two functions. The NextCustomer function moves forward through the stack one row at a time; the PreviousCustomer function moves backward through the stack one row at a time.

Note that the browser functions are not called by any code in the procedure but instead are invoked by a trigger from the Winform. (In this case, a user selects the Prev or Next buttons on the screen or presses the corresponding F7 or F8 keys.) After the function executes, control returns to the Winform. When the user exits the Winform (by pressing PF4), control passes to the command that follows the WINFORM SHOW command.

MAINTAIN FILE VIDEOTRK
FOR ALL NEXT CustID INTO CustInfo
    WHERE ExpDate GE 920601 AND ExpDate LE 920621;
WINFORM SHOW ShowCust;
FOR ALL UPDATE FirstName LastName Street City State Zip 
    Phone FROM CustInfo;
COMMIT; 
CASE NextCustomer
   IF CustInfo.FocIndex LT CustInfo.FocCount THEN
       COMPUTE CustInfo.FocIndex = CustInfo.FocIndex + 1;
ENDCASE
CASE PreviousCustomer
   IF CustInfo.FocIndex GT 1 THEN
       COMPUTE CustInfo.FocIndex = CustInfo.FocIndex - 1;
ENDCASE 
-* >> Generated Code Section....
.
.
.
END

Try it now: If you have created your own set of application files:

  1. Make a copy of your Maintain procedure file, rename it substituting "2" for "1" (for example, TUTOR2 FOCEXEC), and enter these additional commands into it.
  2. Enter FILE at the TED command line to save your file and exit TED.
  3. Run the new procedure (either the version supplied with FOCUS, named VIDTAPE2 or your personal version that you named) at the FOCUS command line.

The Winform appears. Four function keys appear at the bottom of the screen:

Notice that Exit and Close Winform do not have functions in the procedure. They are provided by the Winform facility; no code must be generated for them.


Information Builders