Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

LOAD and SAVE *.DFM

Posted on 2002-06-02
3
Medium Priority
?
1,003 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

597 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