Eggi
Eggi

Reputation: 181

Delphi - TFrame How to save and load component properties

I have a TForm which contains a lot of TFrame's and i want to be able to save all properties like TEdit.text, TCheckbox.checked and so on of the TFrame's to one shared storage. How do i accomplish that with TJvFormStorage and TJvAppIniFileStorage ?

Workflow is the following:

  1. Create a VCL Form, place a TJvAppIniFileStorage there, set the FileName property.
  2. Create a VCL Frame, place a TjvFormStorage there, set the AppStoragePath property.
  3. Add a TEdit onto the frame and add the .text property to the TJvFormStorage with the Form Storage Designer.
  4. Add the Frame to the palette.
  5. In the VCL Form, add the VCL Frame.

I tried to make it happen with passing a TJvAppIniStorage created in the TForm to the TFrame's and to set the .AppStorage of the TJvFormStorage property at runtime. This did not save the predefined properties, but writeString worked.

// FormShow event of the Main Form
procedure TMainForm.FormShow(Sender: TObject);
begin
  frFrame.init(fs_AppIniStorage); // fs_AppIniStorage is the component on the MainForm
end;

// The init function of the TFrame
procedure TfrFrame.init(var FormStorage : TJvAppIniFileStorage);
begin
  fs_FrameStorage.AppStorage := FormStorage;
  fs_FrameStorage.WriteString('test','test'); // <- is written into the storage
end;

This has written the following into the file:

[Test] // fs_FrameStorage.AppStoragePath
test=test// fs_FrameStorage.WriteString

Also i tried to manually save and load the component properties with:

fs_FrameStorage.SaveFormPlacement; // At .FormClose of TMainForm 
fs_FrameStorage.RestoreFromPlacement; // At TfrFrame.init 

After this the file looked like that:

[Test] // fs_FrameStorage.AppStoragePath
test=test// fs_FrameStorage.WriteString
FormVersion=0 // new

Then I tried to set the TFrame.fs_FrameStorage.AppStoragePath in TMainForm in the Delphi Designer.

MainFormProperty

Which resulted into the same.

Calling StoredProps results in the entered edit.text:

ShowMessage(fs_FrameStorage.StoredProps.Text);
// Shows ed_Frame.text

When I add both components on the Same TForm, TMainForm in this case, it works as expected, saving all entered properties. But I can't access the Frames child components from the MainForm with the TJvFormStorage placed in the MainForm.

Upvotes: 1

Views: 742

Answers (1)

Tim Bittner
Tim Bittner

Reputation: 1

Had the same Problem today, but with saving grid related properties.

As TJvFormStorage is a Class of TJvFormPlacement, calling TJvFormPlacement.RestorePlacement and TJvFormPlacement.SavePlacement worked for me.

Here my situation and solution, if anybody else falls into the same pit:

I had one main form with a TJvFormStorage (A) (working like a charm) and a panel, loaded with an external VCL-Frame at runtime, including a TjvUltimGrid. The goal was to load and save the grid-properties into the same AppStorage.

  1. I added a new TJvFormStorage (B) on the frame with the same AppStorage like the main form and set the AppStoragePath by hand
  2. Checking the Options for fpState/fpSize/fpLocation
  3. Then called TJvFormPlacement.RestorePlacement on Storage B after loading the frame
  4. As wells as TJvFormPlacement.SavePlacement on the close event of the main form

Upvotes: 0

Related Questions