The Mathematica Journal
Departments
Download This Issue
Home
Feature Articles
Graphics Gallery
Tricks of the Trade
In and Out
Columns
The Mathematica Programmer
New Products
New Publications
Classifieds
Calendar
News Bulletins
Editor's Pick
Mailbox
Letters
Write Us
About the Journal
Staff and Contributors
Submissions
Subscriptions
Advertising
Back Issues

Creating a Custom Dialog Box

Dale Horton

The Input and InputString commands allow user data input to be entered through an interface. The appearance of the interface is limited, though. Instead of using the simple pre-defined dialog box, you can create a notebook from the kernel and use it as a flexible interface for input.

Mark the Cell That Initiates the Dialog So That the Output Can Be Returned in the Proper Location

[Graphics:../Images/index_gr_16.gif]

This cell identifies the notebook that will receive the output of the dialog and identies a reference point (by marking the cell with a tag) for moving the selection by kernel commands. $SessionID and $Line make CellTags unique to the current evaluation in the current session. This prevents accidentally selecting a different dialog cell.

Create the Notebook Dialog and Fill It with the Desired Cells

[Graphics:../Images/index_gr_17.gif]

The NotebookCreate command is the place to specify any options at the notebook level. The WindowFrameElements option used above removes the "CloseBox" on the window frame that allows users to close notebooks. If the users utilized "CloseBox," the dialog box would be closed without returning the desired information and the original notebook would be unable to continue. Note: the above input cell is for explanation of the fields; it is not evaluatable.

The NotebookWrite commands in this example create a Text cell with a prompt for the user and an empty Input cell into which the user will type. The longer example at the end of this section uses a NotebookWrite to place a button in the dialog notebook window.

The cells in the dialog that you want to manipulate (such as cells you read input from) should have the option CellTags to make them easy to select. You also should have one or more buttons that will exit the dialog and return a result.

Assign Out[$Line] the Return Value of the Dialog

[Graphics:../Images/index_gr_18.gif]

Select the cell (or cells) in the dialog which contain input, read in the contents of those cells, use the input to calculate the output, and then set Out[$Line] to the output of the dialog.

Return the Output of the Dialog to the Original Notebook

[Graphics:../Images/index_gr_19.gif]

This places the output (out) in a cell after the cell with the CellTags we created above. It will be part of the button that closes the dialog.

Put It All Together

Here is a basic example that incorporates the above ideas.

[Graphics:../Images/index_gr_20.gif]

Evaluate the input cell below (MyDialog[]) to create a new notebook window with the prompt, an empty Input cell, and a button. Go to the generated notebook window, enter the argument for Sin in the input cell and click the [Graphics:../Images/index_gr_21.gif] button. This will send the value in the input cell to the original notebook, take the sine of it, and the computed value will appear as the output cell below.

[Graphics:../Images/index_gr_22.gif]
[Graphics:../Images/index_gr_23.gif]

The created notebook contains an instruction prompt, an empty cell where the user can type input, and a Return button to send the output back to calling notebook. After a value is typed into the input cell but before the [Graphics:../Images/index_gr_24.gif] button is clicked, the generated dialog window looks like this:

[Graphics:../Images/index_gr_25.gif]


Converted by Mathematica      October 8, 1999 [Prev Page][Next Page]