Persistent values in rollout (across one session)

Sometimes it is desirable for rollout controls to keep their values when you close the dialog and open it again. There are many ways to achieve that, one of the easiest is using locals declared in a macroscript body and using its on execute handler to initialize the rollout. When a regular script is used instead, a global struct or a global set of values can be used, but there is also another method which might be more suitable in many cases.

A simple example would be keeping a text that was previously entered in an editText control:

try destroyDialog ::persistentRollout catch()

if NOT isKindOf persistentRollout RolloutClass do
    rollout persistentRollout "Persistent Rollout"
        local savedText = if savedText == undefined then "" else savedText

        editText etTest "Type a Value" text:savedText

        on etTest changed text do savedText = text

createDialog persistentRollout

The first line declares a global variable to hold the rollout definition if it haven't been declared yet, and tries to destroy the dialog at the same time. If the global variable contains a rollout class already, we create the dialog with the existing rollout definiton. Otherwise, a rollout is created, and in its scope a local variable to hold the text is declared. When the createDialog function is called for the first time, the local in the rollout haven't been assigned a value yet and so it is assigned an empty string value. When the script is evaluated next time, it already contains a value that can be used.

Of course, for some complex scripts it makes sense to use a struct instead but for small projects it is easy enough to make use of this method.

This Post needs Your Comment!

Return to top