One function to rule them all – Saving the whole GUI one single procedure call

We all created at one point some small utility to do some dirty job for us. It started as a 100 liner then became bigger and bigger. And then we decided to publish it. Let the whole world enjoy it. And this is when disaster hit: the users started to ask more and more features and our 100 liner is not 5000 liner. So now you want to save the GUI state (every checkbox, every radio button, every toggling menu) to disk so the application can user can resume from exactly same point when he restarts the application.

Seems a lot of work to save all 100 graphic elements to an INI file right?
But what if you can do it with one line? What?

In my Delphi Light Saber library you will find the ccINIFileVcl.pas (depends on ccINIFile.pas) unit which does exactly that: save the GUI state to disk simply by calling the SaveForm (Form: TForm) procedure.
To restore the GUI when the application starts again, call LoadForm (Form: TForm).
It really works. No other fuss. No strings attached.

Usage
* Call SaveForm(MyForm) in TMySettingsForm.OnDestroy
* Call LoadForm(MyForm) after the creation of TMySettingsForm


Full demo
Here is a fully functional demo program.

 

Other competencies

Storing individual controls
You can also save individual controls. Example: store the status of a checkbox to INI file with: TCubicIniFileVcl.Write(MyCheckBox).
The user doesn’t have to provide a default value.
If value does not exist in INI file, the value from GUI (MyCheckBox.Checked) will be used.

Saving the form position/size
The SaveForm can take an extra boolean parameter (default False). If True, it will only save the position of the form (Left/Top, no Width/Height/WndState) and not the status of the controls.

Completeness

Most common VCL controls are supported.
Support for more controls can be easily added with just an if/then.
Check IsSupported() to see a list of supported controls.

OnClick execution
The events are executed when the component status is changed when it is read from the ini file ONLY IF its INI staus is different than its DFM status!
In other words, the programmer needs to call the ControlOnClick(sender) after the program loaded.
The TAction.OnExecute is NEVER executed.


The INI file
Use ccIniFile.AppData.IniFile to obtain the file name of the INI file (where to save the data).
Use ccIniFile.AppData.AppDataFolder to get the path where the INI file is stored.
The class will also automatically save the ccAppData.AppData.LastUsedFolder variable


Important
SaveFrom will fail to save the controls of a form if they have been re-parented (moved to another form). But no exception will rise.
The new parent form will also fail to save them.
To fix this, we need to move the controls back to their original parent (form) BEFORE we call SaveFrom!

Notices
* DO NOT USE SELF as parameter in TCubicIniFileEx.Create if you call TCubicIniFileEx.Create in FormCreate because the Self is not yet ready! But TCubicIniFileEx.Create(FrmMain) works.
* LoadForm is called after the components on the form are created but BEFORE their CreateWnd is called!
* This technique should only be used on small projects. For real projects what you should save to disk (maybe to a binary file) is the internal state of the object that is holding the data. When you need to restore that status of the GUI you should read it from the object itself.

UNICODE
INI file does not support Unicode chars. Unicode chars are replaced with ‘?’. Sorry. This is not a limitation in my code but in Delphi’s RTL.

 

Leave a Comment

Scroll to Top