How Do I ... : Work with complex parts : Display message boxes : Tip: Use Smalltalk to add a message box
Tip: Use Smalltalk to add a message box
Display message boxesDisplay message boxesNo tip for this topicNo example for this topicExample: Add a message boxExample: Add a message box
At times, you might find it easier to display a message box using Smalltalk code. For example, if your script tests true or false for a specific condition, you can easily inform the user of the existence (or nonexistence) of that condition by adding a few lines of code to the script and displaying a message box.
Coding message boxes  
The code you add for an informational message box resembles the following:
(CwMessagePrompter new)
title: 'My Message Box';
iconType: XmDIALOGINFORMATION;
buttonType: XmOK;
messageString: 'My message is...';
prompt.
If you type this code into the System Transcript, mark it with your mouse, and select Execute from the Edit menu, you get the following message box:
Message box
For an error message box, you use the same code with the following change:
iconType: XmDIALOGERROR;
And for a warning message box, you use:
iconType: XmDIALOGWARNING;
Programmer Reference explains how to change the icons and buttons of message boxes in "Composite Box Widgets."
Using the code in your script  
Suppose you want to display an error message box if a user enters a value greater than 99. You might use code like the following in a script:
| value |
value := 100.
value > 99
ifTrue: [(CwMessagePrompter new)
title: 'Invalid Number Entered';
iconType: XmDIALOGERROR;
buttonType: XmOK;
messageString: 'You entered ',
value printString,
'. Enter a number between 1 and 99.';
prompt.]
To see what the message box looks like, evaluate it in the System Transcript using Execute.
Note that your script would have a get statement that gets the value from a visual part instead of value := 100. For example, the code would read as follows if it got the value from a Data Entry part named Amount:
| value |
value := ((self subpartNamed: 'Amount') object) asNumber.
value > 99
ifTrue: [ (CwMessagePrompter new)
title: 'Invalid Number Entered';
iconType: XmDIALOGERROR;
buttonType: XmOK;
messageString: 'You entered ',
value printString,
'. Enter a number between 1 and 99.';
prompt.]
Last modified date: 07/25/2020