Real windows applications must be able to "remember" it’s settings like window position and size, selections or the input of text fields when it’s started again. For position and size you can find a bunch of components on the internet. For other things you have to do some aweful inifile or registry operations - not much comfort at all.
PersistentForms does this in the create and destroy methodes of a TForm object. It saves not only the size and position of the form, it saves the properties of the most components on the form too. You can save the information likewise to an inifile or to the registry. In most cases there is no line of source code in your application neccesary.
TPersistentForm is a TForm subclass, that remembers the properties of itself and the properties of the components that are put on it. The properties are saved with the destroy event of the form and reloaded with the create event of the form. The informationen is stored likewise in an inifile or in the registry. Output to Inifile is default.
Download file includes:
- PesistantForms class
- Full source code
To use the class simple put the unit PersitentForms in the USES clausel of the interface part of your form unit. Replace in the declaration of the form class the "class(TForm)" with "class (TPersistentForm)". Example:
type TForm1 = CLASS (TForm) // Without PersistentForm type TForm1 = CLASS (TPersistentForm) // With PersistentForm
With the OnCreate event of the Form you can select where you want to store the form information with the property "direction". Example: Saving to inifile
procedure TForm1.FormCreate(Sender: TObject); begin direction:= tdInifile; (* not necessary, ini file is default! *) end;
The INI file is created in the directory where the program executable is. The name of the inifile is the Application.Title. Example: Saving to registry
procedure TForm1.FormCreate(Sender: TObject); begin direction:= tdRegistry; regkey:= ‘Software/Hellinger’; end;
The default rootkey is HKEY_CURRENT_USER, this can only changed in the source. Like in the example, you only have to define the subkeys of the registry path, PersistentForms adds the program name (Application.Title) automaticly to the path. Each Form that is "persistent" in the application is an own subkey in the application key:
Software/ Hellinger/ Example/ Form1 Form2
Each form key holds the information that is stored. These properties are stored from all components:
- ‘Text’ for TEdits, TCombos etc.
- ‘Checked’ for TCombos, TMenuItems etc.
- ‘Position’ for TrackBars
- ‘InitialDir’ for TOpenDialog, TSaveDialog etc.
- ‘FileName’ for TOpenDialog, TSaveDialog etc.
These properties are saved only from TPanel components ‘TPanel.Top’, ‘TPanel.Left’, ‘TPanel.Height’, ‘TPanel.Width’ From the basic form the position and size is always stored! In the OnCreate event you can easily add more components and properties that should be stored. Simply use the AddFilter methode:
AddFilter (?Enabled?); // to store the enabled property from ALL components AddFilter (’TPanel.Visible’); // to store the visible property of TPanels
Add properties and components as you like, theres no limit. Ok, in fact there is a limit: How many items can you add to a Stringlist?