Difference between revisions of "TXMLPropStorage"

From Lazarus wiki
Jump to navigationJump to search
(Fix link to TINIPropStorage)
 
(17 intermediate revisions by 9 users not shown)
Line 1: Line 1:
 
{{TXMLPropStorage}}
 
{{TXMLPropStorage}}
  
== Introduction ==
+
'''TXMLPropStorage''' [[image:txmlpropstorage.png]] is a component to save and restore selected properties (either [[TForm]] or any control on it). It works with the [[doc:lcl/forms/tform.sessionproperties.html|<tt>TForm.SessionProperties</tt>]] property. It is available on the [[Misc tab]] of the [[Component Palette]].
  
TXMLPropStorage is a component to save and restore selected properties (either TForm or any control on it). It works with the TForm.[[doc:lcl/forms/tform.sessionproperties.html|SessionProperties]] property. It's available on '''Misc''' tab of component palette.
+
== Basic Usage ==
  
== Usage ==
+
[[image:SessionProperties_Editor.png|right]]
# Drop a [[doc:lcl/xmlpropstorage/txmlpropstorage.html|TXMLPropStorage]] component on form and set filename property, for example: session.xml
+
* Drop a <tt>TXMLPropStorage</tt> component on the form and set the <tt>Filename</tt> property as needed, for example: <tt>session.xml</tt>. The <tt>FileName</tt> even can be left empty - see [[#Notes|below]].
# Select form, go to Object Inspector and open editor window for the [[doc:lcl/forms/tform.sessionproperties.html|SessionProperties]] property of TForm.
+
* Select the form, go to the Object Inspector and select the property [[doc:lcl/forms/tform.sessionproperties.html|SessionProperties]]. There are two ways to enter here the properties to be stored:
# Add here properties of form and/or controls to be stored inside session.xml (example: width;height).
+
# Click on the ellipsis button '...' next to the property to open the SessionProperties editor. The components of the form are displayed in the left list "Components". Select the first component for which you want to store a property - the published properties of the selected component appear in the right list, "Properties". Select here the property to be stored and click "Add". Now the property is listed in the bottom listbox, "Selected Properties". Repeat with other properties of the selected component, and repeat with other components. If you changed your mind and want to removed one specific property from the "Selected Properties" list, select it and click "Delete". "Clear" deletes the entire "Selected Properties" list.
# Compile the application.
+
# The selected properties are displayed as a semi-colon separated string in the <tt>SessionProperties</tt> line of the Object Inspector. Instead of using the SessionProperties editor you can also type here the names of the properties to be stored. Subproperties or properties of components on the form must be listed in dot-notation. Example: <tt>Width;Height;Image1.Width</tt>.
 +
* Compile and run the application. Your application now will store the selected properties in the session file when the application terminates, and it will read them when it is restarted another time and it will apply them automatically at runtime.
  
Your application now will read selected property value from session.xml and apply it on runtime (like Width, Height, Left, Top for TForm).
+
The [[TINIPropStorage]] and [[TJsonPropStorage]] components work in the same way as <tt>TXMLPropStorage</tt>. The only difference is that they store the session information in an [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html IniFile] or a JSON file, respectively.
  
TINIPropStorage component works the same way as TXMLPropStorage, except it stores the session information in an [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html IniFile].
+
== StoredValues property ==
  
== StoredValues property ==
+
<tt>TXMLPropStorage</tt> has a <tt>StoredValues</tt> property which allows to store non-published properties or other values; this can be useful to avoid an additional configuration file.
TINIPropStorage and TXMLPropStorage has a ''StoredValues'' property which stores some value (it's useful to uses no others configs file)...
+
 
 +
Let's write a simple demo which uses a <tt>TCheckGroup</tt>. It has a <tt>Checked[index]</tt> property with the information whether the item at the given index is checked or not. This property, however, is only public and thus is not seen by the automatic storage mechanism of the PropStorage components considering only published properties. By means of the <tt>StoredValues</tt> property of <tt>TXMLPropStorage</tt>, however, this can be done manually:
 +
 
 +
* Run Lazarus and start a new application.
 +
* Drop a <tt>TXMLPropStorage</tt> and <tt>TCheckGroup</tt> component.
 +
* Add some items  to the <tt>TCheckGroup</tt>.
 +
* Click in <tt>XMLPropStorage1</tt> and access the <tt>StoredValues</tt> node in the object tree above the object inspector.
 +
* Right-click on this node and select "Add item" to add new value with an arbitrary, but unique <tt>Name</tt> (e.g. <tt>CheckGroup1.Item0_Checked</tt>) and the corresponding initial <tt<Value</tt> = <tt>true</tt>. Repeat accordingly with the other items.
 +
* In the <tt>OnRestoreProperties</tt> event add this code:
 +
 
 +
<syntaxhighlight lang=pascal>
 +
  CheckGroup1.Checked[0] := StrToBool(XMLPropStorage1.StoredValue['CheckGroup1.Item0_Checked']);
 +
  CheckGroup1.Checked[1] := StrToBool(XMLPropStorage1.StoredValue['CheckGroup1.Item1_Checked']);
 +
</syntaxhighlight>
 +
 
 +
* In the <tt>OnSavingProperties</tt> event add this code (do NOT use the <tt>OnSaveProperties</tt>!):
 +
 
 +
<syntaxhighlight lang=pascal>
 +
  XMLPropStorage1.StoredValue['CheckGroup1.Item0_Checked'] := BoolToStr(CheckGroup1.Checked[0], true);
 +
  XMLPropStorage1.StoredValue['CheckGroup1.Item1_Checked'] := BoolToStr(CheckGroup1.Checked[1], true);
 +
</syntaxhighlight>
 +
 
 +
* Run the demo program, change the checked properties of the <tt>CheckGroup</tt>, close the form and re-open it. Your changes are saved and restored!
  
* Why is this really util?
+
You can set a value for the ''KeyString'' property of <tt>StoredValue[n]</tt> if you're saving some confidental information (it uses XOREncode and XORDecode functions of RTL on saving and restoring routines).
*# Some properties (as CheckGroup.Item[n].Checked) cannot be saved in SessionProperties of TForm, then you need do this manually. It's useful to save others settings informations too.
 
  
Let's write a simple demo:
 
  
* Run Lazarus and start a new application;
+
== Property Storage on Frames ==
* Drop a TXMLPropStorage and TCheckGroup component;
 
* Add one item in TCheckGroup (Item Test);
 
* Click in XMLPropStorage1 and access StoredValues property editor;
 
* Add a new value with name = item0_checked and value = -1 (True = -1);
 
* In OnShow event add this code:
 
<source>CheckGroup1.Checked[0] := StrToBool(XMLPropStorage1.StoredValue['item0_checked']);</source>
 
  
* In OnClose event add this code:
+
The <tt>TXMLPropStorage</tt> works only on forms by default. To use it on frames, some manual coding has to be done.
<source>XMLPropStorage1.StoredValue['item0_checked'] := BoolToStr(CheckGroup1.Checked[0]);</source>
+
* Place a <tt>TXMLPropStorage</tt> on the frame.
 +
* Set the property <tt>RootNodePath</tt> to something like <code>TApplication/Frame1</code>. Without setting this property, TXMLPropStorage won't find the saved values upon loading. If you want to reuse the frame multiple times and save the diffrent settings, you should set this in the frame's constructor depending on the situation.
 +
* Set the property <tt>SessionProperties</tt> of the frame in the frames constructor.
 +
* Call <code>Restore</code> in the frame's constructor and <code>Save</code> in the frame's destructor.
  
* Run the demo program, change checked property of TCheckGroup.Items[n] and close form. Your changes was saved? :)
+
<syntaxhighlight lang=pascal>
 +
constructor TFrame1.Create(TheOwner: TComponent);
 +
begin
 +
  inherited Create(TheOwner);
 +
  SessionProperties := 'Panel1.Width;Panel3.Width';
 +
  XMLPropStorage1.RootNodePath := 'TApplication/Frame1'; // if the frame is used multiple times and shall not share their settings, set an individual value for each instance
 +
  XMLPropStorage1.Restore;
 +
end;
  
You can change ''Key'' property of StoredValues.Items[n] if you're saving some information confidential (it uses XOREncode and XORDecode functions of RTL on saving and restoring routines).
+
destructor TFrame1.Destroy;
 +
begin
 +
  XMLPropStorage1.Save;
 +
  inherited Destroy;
 +
end; 
 +
</syntaxhighlight >
  
 
== Notes ==
 
== Notes ==
  
TXMLPropStorage has a default handler if you don't set a filename.
+
<tt>TXMLPropStorage</tt> has a default handler if you don't set a <tt>FileName</tt>.
Under Windows/MacOS the settings will be saved in the application directory as PROGRAMNAME.xml.
+
* Under Windows the settings will be saved in the application directory as PROGRAMNAME.xml.
 
+
* Under Unix/Linux/macOS it will be saved in the home directory of the current user as .PROGRAMNAME
Under Unix likes it will be saved in the home directory of the current user as .PROGRAMNAME
 
  
 
It is therefore a very good idea to leave the filename blank for unix programs meant to be run by normal users.
 
It is therefore a very good idea to leave the filename blank for unix programs meant to be run by normal users.
  
According to bug report [http://bugs.freepascal.org/view.php?id=13949 13949, note 28856]: "The StoredValues[] array can only be used during the OnRestoreProperties or OnSaveProperties events. Outside these events, the values will not be stored." and "If you want to save/load values that are not published properties of a component or control, you should save them in a OnSaveProperties event, and
+
According to bug report [http://bugs.freepascal.org/view.php?id=13949 13949, note 28856]: "The StoredValues[] array can only be used during the OnRestoreProperties or OnSaveProperties events. Outside these events, the values will not be stored." and "If you want to save/load values that are not published properties of a component or control, you should save them in a OnSaveProperties event, and load them using the OnRestoreProperties event."
load them using the OnRestoreProperties event."
 
  
 
== See also ==
 
== See also ==
  
* [[Hardware Access]]
+
* [[TINIPropStorage]]
 +
* [[TJsonPropStorage]]
 +
* [[doc:lcl/xmlpropstorage/txmlpropstorage.html|TXMLPropStorage doc]]
 +
* [[doc:lcl/forms/tform.sessionproperties.html|SessionProperties doc]]  
  
[[Category:Components]]
+
{{LCL Components}}

Latest revision as of 17:39, 14 October 2023

Deutsch (de) English (en) español (es) français (fr) polski (pl) português (pt) русский (ru)

TXMLPropStorage txmlpropstorage.png is a component to save and restore selected properties (either TForm or any control on it). It works with the TForm.SessionProperties property. It is available on the Misc tab of the Component Palette.

Basic Usage

SessionProperties Editor.png
  • Drop a TXMLPropStorage component on the form and set the Filename property as needed, for example: session.xml. The FileName even can be left empty - see below.
  • Select the form, go to the Object Inspector and select the property SessionProperties. There are two ways to enter here the properties to be stored:
  1. Click on the ellipsis button '...' next to the property to open the SessionProperties editor. The components of the form are displayed in the left list "Components". Select the first component for which you want to store a property - the published properties of the selected component appear in the right list, "Properties". Select here the property to be stored and click "Add". Now the property is listed in the bottom listbox, "Selected Properties". Repeat with other properties of the selected component, and repeat with other components. If you changed your mind and want to removed one specific property from the "Selected Properties" list, select it and click "Delete". "Clear" deletes the entire "Selected Properties" list.
  2. The selected properties are displayed as a semi-colon separated string in the SessionProperties line of the Object Inspector. Instead of using the SessionProperties editor you can also type here the names of the properties to be stored. Subproperties or properties of components on the form must be listed in dot-notation. Example: Width;Height;Image1.Width.
  • Compile and run the application. Your application now will store the selected properties in the session file when the application terminates, and it will read them when it is restarted another time and it will apply them automatically at runtime.

The TINIPropStorage and TJsonPropStorage components work in the same way as TXMLPropStorage. The only difference is that they store the session information in an IniFile or a JSON file, respectively.

StoredValues property

TXMLPropStorage has a StoredValues property which allows to store non-published properties or other values; this can be useful to avoid an additional configuration file.

Let's write a simple demo which uses a TCheckGroup. It has a Checked[index] property with the information whether the item at the given index is checked or not. This property, however, is only public and thus is not seen by the automatic storage mechanism of the PropStorage components considering only published properties. By means of the StoredValues property of TXMLPropStorage, however, this can be done manually:

  • Run Lazarus and start a new application.
  • Drop a TXMLPropStorage and TCheckGroup component.
  • Add some items to the TCheckGroup.
  • Click in XMLPropStorage1 and access the StoredValues node in the object tree above the object inspector.
  • Right-click on this node and select "Add item" to add new value with an arbitrary, but unique Name (e.g. CheckGroup1.Item0_Checked) and the corresponding initial <tt<Value = true. Repeat accordingly with the other items.
  • In the OnRestoreProperties event add this code:
  CheckGroup1.Checked[0] := StrToBool(XMLPropStorage1.StoredValue['CheckGroup1.Item0_Checked']);
  CheckGroup1.Checked[1] := StrToBool(XMLPropStorage1.StoredValue['CheckGroup1.Item1_Checked']);
  • In the OnSavingProperties event add this code (do NOT use the OnSaveProperties!):
  XMLPropStorage1.StoredValue['CheckGroup1.Item0_Checked'] := BoolToStr(CheckGroup1.Checked[0], true);
  XMLPropStorage1.StoredValue['CheckGroup1.Item1_Checked'] := BoolToStr(CheckGroup1.Checked[1], true);
  • Run the demo program, change the checked properties of the CheckGroup, close the form and re-open it. Your changes are saved and restored!

You can set a value for the KeyString property of StoredValue[n] if you're saving some confidental information (it uses XOREncode and XORDecode functions of RTL on saving and restoring routines).


Property Storage on Frames

The TXMLPropStorage works only on forms by default. To use it on frames, some manual coding has to be done.

  • Place a TXMLPropStorage on the frame.
  • Set the property RootNodePath to something like TApplication/Frame1. Without setting this property, TXMLPropStorage won't find the saved values upon loading. If you want to reuse the frame multiple times and save the diffrent settings, you should set this in the frame's constructor depending on the situation.
  • Set the property SessionProperties of the frame in the frames constructor.
  • Call Restore in the frame's constructor and Save in the frame's destructor.
constructor TFrame1.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  SessionProperties := 'Panel1.Width;Panel3.Width';
  XMLPropStorage1.RootNodePath := 'TApplication/Frame1'; // if the frame is used multiple times and shall not share their settings, set an individual value for each instance
  XMLPropStorage1.Restore;
end;

destructor TFrame1.Destroy;
begin
  XMLPropStorage1.Save;
  inherited Destroy;
end;

Notes

TXMLPropStorage has a default handler if you don't set a FileName.

  • Under Windows the settings will be saved in the application directory as PROGRAMNAME.xml.
  • Under Unix/Linux/macOS it will be saved in the home directory of the current user as .PROGRAMNAME

It is therefore a very good idea to leave the filename blank for unix programs meant to be run by normal users.

According to bug report 13949, note 28856: "The StoredValues[] array can only be used during the OnRestoreProperties or OnSaveProperties events. Outside these events, the values will not be stored." and "If you want to save/load values that are not published properties of a component or control, you should save them in a OnSaveProperties event, and load them using the OnRestoreProperties event."

See also


LCL Components
Component Tab Components
Standard TMainMenu • TPopupMenu • TButton • TLabel • TEdit • TMemo • TToggleBox • TCheckBox • TRadioButton • TListBox • TComboBox • TScrollBar • TGroupBox • TRadioGroup • TCheckGroup • TPanel • TFrame • TActionList
Additional TBitBtn • TSpeedButton • TStaticText • TImage • TShape • TBevel • TPaintBox • TNotebook • TLabeledEdit • TSplitter • TTrayIcon • TControlBar • TFlowPanel • TMaskEdit • TCheckListBox • TScrollBox • TApplicationProperties • TStringGrid • TDrawGrid • TPairSplitter • TColorBox • TColorListBox • TValueListEditor
Common Controls TTrackBar • TProgressBar • TTreeView • TListView • TStatusBar • TToolBar • TCoolBar • TUpDown • TPageControl • TTabControl • THeaderControl • TImageList • TPopupNotifier • TDateTimePicker
Dialogs TOpenDialog • TSaveDialog • TSelectDirectoryDialog • TColorDialog • TFontDialog • TFindDialog • TReplaceDialog • TTaskDialog • TOpenPictureDialog • TSavePictureDialog • TCalendarDialog • TCalculatorDialog • TPrinterSetupDialog • TPrintDialog • TPageSetupDialog
Data Controls TDBNavigator • TDBText • TDBEdit • TDBMemo • TDBImage • TDBListBox • TDBLookupListBox • TDBComboBox • TDBLookupComboBox • TDBCheckBox • TDBRadioGroup • TDBCalendar • TDBGroupBox • TDBGrid • TDBDateTimePicker
Data Access TDataSource • TCSVDataSet • TSdfDataSet • TBufDataset • TFixedFormatDataSet • TDbf • TMemDataset
System TTimer • TIdleTimer • TLazComponentQueue • THTMLHelpDatabase • THTMLBrowserHelpViewer • TAsyncProcess • TProcessUTF8 • TProcess • TSimpleIPCClient • TSimpleIPCServer • TXMLConfig • TEventLog • TServiceManager • TCHMHelpDatabase • TLHelpConnector
Misc TColorButton • TSpinEdit • TFloatSpinEdit • TArrow • TCalendar • TEditButton • TFileNameEdit • TDirectoryEdit • TDateEdit • TTimeEdit • TCalcEdit • TFileListBox • TFilterComboBox • TComboBoxEx • TCheckComboBox • TButtonPanel • TShellTreeView • TShellListView • TXMLPropStorage • TINIPropStorage • TJSONPropStorage • TIDEDialogLayoutStorage • TMRUManager • TStrHolder
LazControls TCheckBoxThemed • TDividerBevel • TExtendedNotebook • TListFilterEdit • TListViewFilterEdit • TLvlGraphControl • TShortPathEdit • TSpinEditEx • TFloatSpinEditEx • TTreeFilterEdit • TExtendedTabControl •
RTTI TTIEdit • TTIComboBox • TTIButton • TTICheckBox • TTILabel • TTIGroupBox • TTIRadioGroup • TTICheckGroup • TTICheckListBox • TTIListBox • TTIMemo • TTICalendar • TTIImage • TTIFloatSpinEdit • TTISpinEdit • TTITrackBar • TTIProgressBar • TTIMaskEdit • TTIColorButton • TMultiPropertyLink • TTIPropertyGrid • TTIGrid
SQLdb TSQLQuery • TSQLTransaction • TSQLScript • TSQLConnector • TMSSQLConnection • TSybaseConnection • TPQConnection • TPQTEventMonitor • TOracleConnection • TODBCConnection • TMySQL40Connection • TMySQL41Connection • TMySQL50Connection • TMySQL51Connection • TMySQL55Connection • TMySQL56Connection • TMySQL57Connection • TSQLite3Connection • TIBConnection • TFBAdmin • TFBEventMonitor • TSQLDBLibraryLoader
Pascal Script TPSScript • TPSScriptDebugger • TPSDllPlugin • TPSImport_Classes • TPSImport_DateUtils • TPSImport_ComObj • TPSImport_DB • TPSImport_Forms • TPSImport_Controls • TPSImport_StdCtrls • TPSCustomPlugin
SynEdit TSynEdit • TSynCompletion • TSynAutoComplete • TSynMacroRecorder • TSynExporterHTML • TSynPluginSyncroEdit • TSynPasSyn • TSynFreePascalSyn • TSynCppSyn • TSynJavaSyn • TSynPerlSyn • TSynHTMLSyn • TSynXMLSyn • TSynLFMSyn • TSynDiffSyn • TSynUNIXShellScriptSyn • TSynCssSyn • TSynPHPSyn • TSynTeXSyn • TSynSQLSyn • TSynPythonSyn • TSynVBSyn • TSynAnySyn • TSynMultiSyn • TSynBatSyn • TSynIniSyn • TSynPoSyn
Chart TChart • TListChartSource • TRandomChartSource • TUserDefinedChartSource • TCalculatedChartSource • TDbChartSource • TChartToolset • TChartAxisTransformations • TChartStyles • TChartLegendPanel • TChartNavScrollBar • TChartNavPanel • TIntervalChartSource • TDateTimeIntervalChartSource • TChartListBox • TChartExtentLink • TChartImageList
IPro TIpFileDataProvider • TIpHtmlDataProvider • TIpHttpDataProvider • TIpHtmlPanel
Virtual Controls TVirtualDrawTree • TVirtualStringTree • TVTHeaderPopupMenu