Solved

LOAD and SAVE *.DFM

Posted on 2002-06-02
3
986 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now