How Do I...

Tip: Use Smalltalk to add a message box

Display message boxes No tip for this topic No example for this topic Example: 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;
 

IBM Smalltalk Programmer's 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.]
 


[ Top of Page | Previous Page | Next Page | Table of Contents ]