* * *

Author Topic: save and restore a form for lazarus  (Read 1108 times)

nana232

  • New member
  • *
  • Posts: 13
save and restore a form for lazarus
« on: March 18, 2017, 07:16:38 am »
I found the Delphi's method in Stackover flow from the following link:
http://stackoverflow.com/questions/3163586/how-to-save-and-restore-a-form


Quote
PRUZ's solution is a ready made solution; JVCL is open-source, and using JvFormStorage is simple. But you can also use Delphi's own streaming mechanism without using any third-party components. Here is an example:

Code: Pascal  [Select]
  1. procedure SaveComponentToFile(Component: TComponent; const FileName: TFileName);
  2. var
  3.   FileStream : TFileStream;
  4.   MemStream : TMemoryStream;
  5. begin
  6.   MemStream := nil;
  7.  
  8.   if not Assigned(Component) then
  9.     raise Exception.Create('Component is not assigned');
  10.  
  11.   FileStream := TFileStream.Create(FileName,fmCreate);
  12.   try
  13.     MemStream := TMemoryStream.Create;
  14.     MemStream.WriteComponent(Component);
  15.     MemStream.Position := 0;
  16.     ObjectBinaryToText(MemStream, FileStream);
  17.   finally
  18.     MemStream.Free;
  19.     FileStream.Free;
  20.   end;
  21. end;

SaveComponentToFile takes a component instance, plus a file name, and streams the component into the file, in a human-readable text.

To load the component from file, you can use a code like this:

Code: Pascal  [Select]
  1. procedure LoadComponentFromFile(Component: TComponent; const FileName: TFileName);
  2. var
  3.   FileStream : TFileStream;
  4.   MemStream : TMemoryStream;
  5.   i: Integer;
  6. begin
  7.   MemStream := nil;
  8.  
  9.   if not Assigned(Component) then
  10.     raise Exception.Create('Component is not assigned');
  11.  
  12.   if FileExists(FileName) then
  13.   begin
  14.     FileStream := TFileStream.Create(FileName,fmOpenRead);
  15.     try
  16.       for i := Component.ComponentCount - 1 downto 0 do
  17.       begin
  18.         if Component.Components[i] is TControl then
  19.           TControl(Component.Components[i]).Parent := nil;
  20.         Component.Components[i].Free;
  21.       end;
  22.  
  23.       MemStream := TMemoryStream.Create;
  24.       ObjectTextToBinary(FileStream, MemStream);
  25.       MemStream.Position := 0;
  26.       MemStream.ReadComponent(Component);
  27.       Application.InsertComponent(Component);
  28.     finally
  29.       MemStream.Free;
  30.       FileStream.Free;
  31.     end;
  32.   end;
  33. end;
  34.  

LoadComponentFromFile takes a component instance, and a file name, then loads file content into the component instance. To avoid naming conflict, we are free all existing owned components of the instance, before loading file data into it.

Now you can use the above code for saving a form into a file

Code: Pascal  [Select]
  1. SaveComponentToFile(FSecondForm,ExtractFilePath(Application.ExeName)+ 'formdata.txt');

FSecondForm is a form instance, and it will be saved into "formdata.txt" file inside the same folder as the EXE file.

And to load FSecondForm from "formdata.txt" file, we write this:

Code: Pascal  [Select]
  1.   if not Assigned(FSecondForm) then
  2.     FSecondForm := TfrmSecond.Create(Application);
  3.   LoadComponentFromFile(FSecondForm,ExtractFilePath(Application.ExeName)+ 'formdata.txt');
  4.   FSecondForm.Show;

LoadComponentFromFile needs the instance to be created first, so we check if FSecondForm is assigned, if not, we create an instance of it (it is an instance of TfrmSecond class), and then load file data into it. And eventually, we show the loaded form.


From ... vcldeveloper



I 've tried these codes in Lazarus with my simple Form:

[Form1 , Edit1,2,3 , and two buttons (SaveBtn aand LoadBtn)]

The app can save and load all components properly but still have Error of Duplicate Name ("Form1_1")
and External SIGSEGV.
The error may occur from the last two codes, because I replaced "FSecondForm" with" Form1"
and and "TfrmSecond" by "TForm1" as shown below:

Quote
Code: Pascal  [Select]
  1. SaveComponentToFile(Form1,ExtractFilePath(Application.ExeName)+ 'formdata.txt');


Code: Pascal  [Select]
  1.   if not Assigned(Form1) then
  2.     Form1 := TForm1.Create(Application);
  3.   LoadComponentFromFile(Form1,ExtractFilePath(Application.ExeName)+ 'formdata.txt');
  4.   Form1.Show;



Could anyone please tell me " How to modify these codes?".

Thank you
 
Lazarus 1.6.4 Win10 64bit

sky_khan

  • Guest
Re: save and restore a form for lazarus
« Reply #1 on: March 18, 2017, 09:06:54 am »
if Form1 is your Application's mainform, it is already assigned so you are running only these lines
Code: Pascal  [Select]
  1. LoadComponentFromFile(Form1,ExtractFilePath(Application.ExeName)+ 'formdata.txt');
  2. Form1.Show;
  3.  

therefore, as Form1 is already exists, you are trying to load its child components and insert into Application, again! No wonder it complain about duplicates.

What are you trying to accomplish ?

nana232

  • New member
  • *
  • Posts: 13
Re: save and restore a form for lazarus
« Reply #2 on: March 18, 2017, 06:34:46 pm »
if Form1 is your Application's mainform, it is already assigned so you are running only these lines
Code: Pascal  [Select]
  1. LoadComponentFromFile(Form1,ExtractFilePath(Application.ExeName)+ 'formdata.txt');
  2. Form1.Show;
  3.  

therefore, as Form1 is already exists, you are trying to load its child components and insert into Application, again! No wonder it complain about duplicates.

What are you trying to accomplish ?

Actually, I would like to save all components and variables which are contained in MainForm.
Lazarus 1.6.4 Win10 64bit

howardpc

  • Hero Member
  • *****
  • Posts: 2251
Re: save and restore a form for lazarus
« Reply #3 on: March 18, 2017, 09:10:08 pm »
Actually, I would like to save all components and variables which are contained in MainForm.

The IDE does this for you.
The mainform.lfm saves a text representation of the main TForm class descendant, and a text representation of all the controls it contains.
The variables are all named in the main form's .pas (or .pp) file. You could parse this to look for, say, just the global variables (if that is what you are after).
However, getting the variables' values would be trickier. Being variable, the values ... vary. So if you are after variable value data you'll have to be more specific about at what point in the app's lifecycle you wanted the variables' values, and if you wanted only the values of simple variables, or also values of records, arrays, and non-GUI classes etc.

Saving this data is fairly straightforward, as the code you showed indicates. But the IDE does a perfect job of completing this task, so I don't see the point of reinventing that wheel.

On the other hand the code you show for reconstructing a form and its components is nonsense. On the pretext of avoiding name clashes the code tries to strip out all the form's contents.
What good is a reconstructed empty form? You might just as well call Application.CreateForm(xx, yy); and be done with it.
Presumably the point of the code you found was to save and later rebuild an entire form. This is a non-trivial exercise, and the IDE Designer devotes hundreds if not thousands of lines to this task, since this is exactly what the IDE has to do when it opens a GUI project. Don't imagine that it is a ten-liner to rebuild a form from its .pas and .lfm files and recursively construct each control and any sub controls and get them all parented properly after finding all the required registered classes.

nana232

  • New member
  • *
  • Posts: 13
Re: save and restore a form for lazarus
« Reply #4 on: March 20, 2017, 07:51:55 am »
Thanks for all answers.
 :)
Lazarus 1.6.4 Win10 64bit

sky_khan

  • Guest
Re: save and restore a form for lazarus
« Reply #5 on: March 20, 2017, 12:18:18 pm »
Using component streaming to save data is kind of messy and not reliable in long term. Classes and properties change by every version of Lazarus etc.

If you're trying to save form properties you may use FormStorage components like TXMLPropStorage or TIniPropStorage. They're on Misc tab.

e.g: Put a TXMLPropStorage on your form and use your Form's SessionProperties property to save properties you want to keep between sessions of your program

http://wiki.freepascal.org/TXMLPropStorage

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus