?
Solved

LOAD and SAVE *.DFM

Posted on 2002-06-02
3
Medium Priority
?
999 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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