17. Message Box
Description
How to Display a Message Box with a Dynamic Message.
Code
Explanation
Simple Message Box: The easiest way to create a message box is with additional text. Create a dialog called MessageBox. In the "Addition Text" area type "<<Message-t>>" (without quotation marks). The "less than" and "greater than" symbols will change to chevrons. Now drag this up onto the list of variables. The dialog is done.
Next create a text variable called Message-t. Set its Advanced options to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file". Don't be tempted to place this variable directly on the MessageBox dialog. Only the additional text you created should be there.
Whenever you want to display a message, all you need to do is SET the value of Message-t and then ASK MessageBox. The additional text on the dialog will be filled with the value of Message-t. The message can be as long as you wish. The following examples show some ways you can use the message box.
Note that you can use returns and tabs in the message. See Computation 12: Returns in Text Strings, and Computation 59: Tabs in Text Strings for details.
Code
""
ASK Dialog
IF MCVar = "Option A"
SET Message-t TO "Warning: You selected Option A. You know better. Try again."
ASK MessageBox
ASK Dialog
ELSE IF MCVar = "Option B"
SET Message-t TO "Excellent choice! You may proceed."
ASK MessageBox
END IF
*** Repeat the above IF ... END IF block exactly to give the user more than one chance ***
*** Optionally end with this excessive tries warning: ***
IF MCVar = "Option A"
SET Message-t TO "You are impossible. Please click 'Cancel' and end this madness."
ASK MessageBox
ASK NONE
ELSE IF MCVar = "Option B"
SET Message-t TO "Excellent choice! You may proceed."
ASK MessageBox
END IF
Explanation
This computation uses the message box method described above to display a message to the user after they answer a multiple choice variable.
The computation ASKs a dialog then tests the user's answers after they click "Next". If a given answer is incorrect, they are given a warning and then taken back to the dialog to try again. If they repeatedly mis-answer, the assembly is aborted (sort of) with an ASK NONE. Only if they answer correctly are they allowed to proceed.
Since computations process very quickly, you don't need to be stingy with the number of IF statements you put in the computation (i.e. the number of chances you'll give the user). If you prefer shorter computations, try the recursive example below.
Code
""
// Initialize the fail count
IF NOT ANSWERED( FailCount-n )
SET FailCount-n TO 0
END IF
// ASK the dialog
ASK Dialog
// Bad answer
IF Variable = "Some bad value"
SET FailCount-n TO FailCount-n + 1
IF FailCount-n <= 3
SET Message-t TO "That is an impermissible answer. Failure «FailCount-n» of 3."
ASK MessageBox
ThisComputation // Replace with the name of this computation
// Too many bad answers
ELSE
SET FailCount-n TO 0
SET Message-t TO "Maximum failure count reached. Aborting assembly."
ASK MessageBox
ASK NONE
END IF
// Correct answer
ELSE
SET Message-t TO "Good Answer! You may proceed."
ASK MessageBox
SET FailCount-n TO 0
END IF
Explanation
This example shows how to use a "recursive" computation for the message box routine. A recursive computation is one that calls itself over and over.
The example uses a temporary number variable called FailCount-n (be sure to set its Advanced options to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file"). This variable tracks how many times the user has given a bad answer. Each time a bad answer is given, the value of FailCount-n is increased by one and the message box is displayed. When the user clicks Next on the message box, the same computation is called again. This gives the user a chance to change their answer and it causes their answer to be checked again (and again, and again). When the user reaches the maximum of three failures, assembly aborts (with ASK NONE). If at any point the user provides a good answer, assembly will proceed as normal.
Explanation