TXMLPropStorage/ru

From Lazarus wiki
Revision as of 16:47, 21 April 2010 by Vadim (talk | contribs) (→‎Usage)
Jump to navigationJump to search

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

Введение

TXMLPropStorage - это компонент для того, чтобы сохранять и восстанавливать выбранные свойства (TForm или любого элемента управление на нем). Это работает совместно со свойством TForm.SessionProperties. Оно доступно на вкладке "Misc" палитры компонентов.

Usage

  1. Положите компонент [doc:lcl/xmlpropstorage/txmlpropstorage.html|TXMLPropStorage] на форму и установите свойство "filename" в, например, 'session.xml'.
  2. Выберите форму, зайдите в Инспектор Объектов и откройте окно редактора у свойства TForm.[doc:lcl/forms/tform.sessionproperties.html|SessionProperties].
  3. Добавьте здесь свойства формы и/или элемента управлений, которые будут сохранены внутри session.xml (пример: width;height).
  4. Скомпилируйте приложение.

Ваше приложение теперь будет читать выбранное значение свойства в session.xml и применять его во время выполнения (такие как Width, Height, Left, Top для TForm).

Компонент TINIPropStorage работает так же, как и TXMLPropStorage, но кроме этого хранит и информацию о сеансе в IniFile.

StoredValues property

TINIPropStorage and TXMLPropStorage has a StoredValues property which stores some value (it's useful to uses no others configs file)...

  • Why is this really util?
    1. 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;
  • 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:

<delphi> CheckGroup1.Checked[0] := StrToBool(XMLPropStorage1.StoredValue['item0_checked']); </delphi>

  • In OnClose event add this code:

<delphi> XMLPropStorage1.StoredValue['item0_checked'] := BoolToStr(CheckGroup1.Checked[0]); </delphi>

  • Run the demo program, change checked property of TCheckGroup.Items[n] and close form. Your changes was saved? :)

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).

Notes

TXMLPropStorage has a default handler if you don't set a filename. Under Windows/MacOS the settings will be saved in the application directory as PROGRAMNAME.xml.

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.

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."


Hardware Access