Solved

LOAD and SAVE *.DFM

Posted on 2002-06-02
3
995 Views
Last Modified: 2011-09-20
LOAD and SAVE *.DFM

How to load and save FORMs in DFMs at runtime ?

Thanks.
0
Comment
Question by:cvbmn
  • 2
3 Comments
 
LVL 1

Expert Comment

by:bes67
ID: 7050086
Class TFileStrem has methods to load and save components. There are ReadComponent and WriteComponent and ReadComponentRes and WriteComponentRes. Besides, there are WriteComponentResFile and ReadComponentResFile functions.
When you are ready to save your form just write

WriteComponentResFile(‘Form1.frm’, self);

where first parameter is a filename and path of a form file (where to save) and a second parameter is a reference to form (use self while calling from form method)

When you want to load previously saved form do something like this

AForm:=(AFormClass).CreateNew(AOwner); // creates a form instance
ReadComponentResFile(aFileName, AForm); // reads properties

Hope this will help
0
 

Author Comment

by:cvbmn
ID: 7050754
WriteComponentResFile is not working  ! ! ! ! !

I tried :

WriteComponentResFile('Form.frm', TForm(Componentlist[0]));

But it doesn't work.
Because Form.frm size is 1kb, but on that form are about 30-40 components.

I exactly need dfm TEXT file to save form.
Or info with RICHEDIT where is DFM TEXT code of choosen FORM.

Thanks.
0
 
LVL 1

Accepted Solution

by:
bes67 earned 300 total points
ID: 7050968
Here I make a try to write two procedures - one to save form as text and another to read form as text

procedure WriteFormAsText(aFileName: string; aForm: TForm);
var MemoStream:TMemoryStream;
    FileStream: TFileStream;
    s: string;
begin
  FileStream := TFileStream.Create(aFileName,fmCreate);
  try
    MemoStream := TMemoryStream.Create;
    try
      MemoStream.WriteComponent(aForm);
      MemoStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(MemoStream, FileStream);
    finally
      MemoStream.Free;
    end;
  finally
    FileStream.Free
  end;
end;

procedure ReadFormAsText(aFileName: string; aForm: TForm);
var FileStream: TFileStream;
    MemoStream: TMemoryStream;
begin
  FileStream := TFileStream.Create(aFileName, fmOpenRead);
  try
    MemoStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(FileStream, MemoStream);
      MemoStream.Seek(0, soFromBeginning);
      MemoStream.ReadComponent(aForm);
    finally
      MemoStream.Free;
    end;
  finally
    FileStream.Free;
  end;
end;

To use them write code like this

Saving:


var frm: TForm;
...
begin
...
  frm := Application.FindComponent('Form1') as TForm;
  WriteFormAsText('Test.frm',frm);
...
end;

Reading:

var frm: TForm;
...
begin
...
  Frm := TForm1.CreateNew(Application);
  ReadFormAsText('Test.frm',frm);
...
end;

There is one thing you have to take into account: if you call WriteFormAstext for a form while it is visible then the procedure will store form's property Visible = True and after reading your form with ReadFormAsText it will be shown on the screen.

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 2 77
creating threads in delphi 1 166
Microsoft Access 97 and Delphi XE2 9 74
How to make Sign in, using Clientdataset? 1 33
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question