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
Solved

LOAD and SAVE *.DFM

Posted on 2002-06-02
3
993 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Machine not responding during CopyFile() 3 103
CheckListBox usage 3 71
Twebbrowser add css to the header 3 27
firemonkey Android Listview Sort items 7 47
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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 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