How Can I Pass Data To and Get Data From a Dialog Window?
LiveCode has built-in support for basic dialogs using the answer and ask commands. But sometimes your application needs to display a customized dialog to the user. The dialog may prompt for some user input or merely alert the user about something. In this case you need to create a stack that acts as your modal dialog (you can make the dialog stack as a substack of your main program stack).
This lesson will show you how to use the dialogData global property to pass data from the handler that opens the dialog to the modal dialog stack itself, and then back again. The dialogData is a special global property reserved especially for passing data back and forth between a dialog window and the handler that opens the dialog window.
You can download the sample stack from this url: https://tinyurl.com/y8canqx8
In order to see how the dialogData global property works I will show you how to open a dialog by clicking on a button (1). The dialog that opens will display a list of choices (2). The item that is initially selected (3) will be provided by the main application window when the button is clicked.
When the dialog window is closed (4) the item that was selected will be passed back to handler that opened the dialog.
Opening The Dialog
Let's start by looking at the code that opens a dialog. When clicking on the Open Dialog button (1) the stack My Modal Window will be opened as a modal window (2). Before opening the window, however, the global property the dialogData will be assigned the name of the item that should be selected by default (3) when the dialog opens.
Selecting the Default Choice In The Modal Dialog List
Because a global property (remember that the dialogData is a global property) is available in all of your scripts the preOpenCard handler in the My Modal Window card script (the only card of the My Modal Window stack) has access to it.
The modal dialog uses the value stored in the dialogData (1) to select the default choice in the list field (2).
Passing The Selected Choice Back To The Calling Handler
Data can be passed from the dialog window back to the handler that opened the dialog window just as easily. Before closing the dialog window just set the dialogData to the value you want to return (1).
In this case the dialogData will be set to the text that is selected in the list (2). You can set the dialogData to any value though.
## Checkbox set the dialogData to the hilite of button "MyCheckBox" ## Group with radio buttons set the dialogData to the hilitedButtonName of group "MyRadioButtons" ## Text field set the dialogData to the text of field "MyField"
If your dialog allows multiple inputs you can pass each value back on separate lines or using an array (go to the end of this lesson for more information on using arrays).
set the dialogData to the hilite of button "MyCheckBox" & cr & the hilitedButtonName of group "MyRadioButtons"
Displaying Selected Choice
Here is the Open Dialog button script again with a slight modification. Now after the modal dialog window is closed, and thus execution of the mouseUp handler continues, the value of the dialogData is displayed in an answer dialog (1).
Here is the end result.
1) User clicks on the Open Dialog button.
2) "Item 2" is set as the default choice and is selected in the modal dialog that appears.
3) The user makes a selection in the list.
4) The user clicks the Close button.
5) And an answer dialog displays the selection that was made.
Additional Notes: Using Arrays With The dialogData
You can also pass arrays in the dialogData global property. Using arrays to pass data in the dialogData can be very useful when passing multiple values. The keys to using arrays are:
1) To assign an array to the dialogData create a variable that is an array and assign that variable to the dialogData.
2) To get an array out of the dialogData put the dialogData value into a variable and then use the variable.
The following LiveCode will display the answer dialog pictured here.
-- Assign an array to the dialogData put "a string" into theA["a string"] put 0 into theA["a number"] -- Store the array in the dialogData set the dialogData to theA -- Get an array from the dialogData put the dialogData into theA -- Display keys of the array answer theA["a string"] & cr & theA["a number"]