6.30 Remember the User's Choices
When your application contains options and choices that your users are likely to want to change, make sure that they need to change them only once by recording them.
Similarly, often when users start up an application, they want to continue working with the same data they were working with when they last used the application. To make this easy the application should start up with the data that was being displayed when it was last shut down.
Storing Settings in the Windows Registry
To store information about a form in the registry and then retrieve it, use the Change command with one of these special remembered values:
*remembered_value_for_user |
Returns the value a field had when a particular user last executed this form |
*remembered_value_for_user_in_system |
Returns the value a field had when a particular user last executed any form or a function containing this field |
*remembered_value_for_function |
Returns the value a field had when this form was last executed |
*remembered_value_for_system |
Returns the value a field had when any form or function containing the field was last executed. |
The first time you execute a Change command with one of these values, a registry entry for the last value of the field in question is created.
Note: These values are not available with a web interface as, for security purposes, they will not have access to registry settings.
Example
Our sample application is used to retrieve the name of an employee based on the employee number:
When the application is started, it shows the employee that was being displayed when the application was last closed down. The values are retrieved using this code:
EVTROUTINE handling(#com_owner.Initialize)
CHANGE FIELD(#EMPNO #GIVENAME #SURNAME) TO(*remembered_value_for_function)
ENDROUTINE
Remembering the Form's Size and Position
You use this same method to remember a form's position and size, but first you need to create fields for storing the information:
Define #top *dec 7 0
Define #left *dec 7 0
Define #width *dec 7 0
Define #height *dec 7 0
Then you need to retrieve the last values of these fields and assign them to the appropriate properties of the form in the Initialize event:
EVTROUTINE handling(#com_owner.Initialize)
CHANGE FIELD(#top #left #width #height) TO(*remembered_value_for_function)
Set #com_owner top(#top) left(#left) width(#width) height(#height)
ENDROUTINE
To store the current values of these four properties to the fields when the application is closed, you need to specify:
EVTROUTINE handling(#com_owner.closing)
Change #top #com_owner.top
Change #left #com_owner.left
Change #width #com_owner.width
Change #height #com_owner.height
Endroutine
Simplifying the Code
You can simplify your code by grouping together all the fields you want the application to remember. You could call the group for example #Remember:
group_by name(#remember) fields(#top #left #width #height #empno #givename #surname)
and then simply add this line to the Initialize event of the form:
Change #remember *remembered_value_for_user
Making All Forms Remember
You may want to consider adding the code required for remembering settings to the STD_FORM component so that it will be in every new form you create. For more information refer to 6.10.4 Modify Default Component Behavior.