Solved

Save component's status--A Simple question?

Posted on 2000-04-02
10
168 Views
Last Modified: 2010-04-04
hi friends:
    I want a common way to save all component's status (including captions, colors, posions ect.). I know I can write them into registry or ini file one by one, but I really do not want to do so. I want a easy way like:
for i:=0 to componentcount-1
begin
 with components[i] do
 begin
  for j := 0 to propertisecount - 1
   SavePropertise(Component[i],propertise[j]);
 end;
end;

I want the answer in 1 days, else I'll delete the question, thanx a lot.
I just want code not component, thanx again.
0
Comment
Question by:chengjian
  • 3
  • 2
  • 2
  • +2
10 Comments
 

Expert Comment

by:hoschmi
Comment Utility
you cant write data in your compiled exe-file!
you must use a configuration or ini-file, in which all data is written e.g. onClose.use a configuration type like
 Tconfiguration = record
     text1 : string;
     text2 : string;
     ....
   end;

then make a variable
 config : Tconfiguration;

write everything in it onClose like
  config.text1:=edit1.text;
 ...

then save it in your config file like
savefile : file of Tconfiguration;
....
write(savefile,config)
....


0
 

Expert Comment

by:Carpathia
Comment Utility
I dont think he wants to write the data to the exe, he just doesnt want to use ini or registry.

Try looking at the ReadComponent, and WriteComponent methods of TStream, or in this case, TFileStream.

Hope this helps
0
 

Expert Comment

by:Carpathia
Comment Utility
One more thing..

For custom properties you might need to add the stored; directive after the property definition.
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
Marco Cantu's example of saving/restoring
ANY component, including an text EditBox (if that is
what you mean), using TStream.  The component be an
entire form full of components.  As long as the component's properties are published (i.e. the text in an
 edit box), it will work....and you have a persistent object  
 
from Marco Cantu, "Delphi Developer's Handbook",

Example assumes the component to be stored is an object named OutputForm....
 

var
  Str1 : TFileStream;
begin
   begin
    Str1 := TFileStream.Create (SomeFileName,
      fmOpenWrite or fmCreate);
try
      Str1.WriteComponentRes (         OutputForm.ClassName, OutputForm);//save the entire component
    finally
      Str1.Free;
    end;
  end;
end;
 
AND to read
 
var
  Str1: TFileStream;
  TempForm1: TOutputForm;
begin
    Str1 := TFileStream.Create (SomeFileName,
      fmOpenRead); //open a stream
    try
      TempForm1 := TOutputForm.Create (Application); //temp object       Str1.ReadComponentRes (TempForm1); OutputForm.Free; //destroy the existing component
      OutputForm := TempForm1;        
OutputForm.Show;      
    finally
      Str1.Free;
    end;
  end;
end;


See also, Earl F Glynn's links about
   Streams at
http://www.efg2.com/Lab/Library/Delphi/IO/StreamIO.htm
0
 
LVL 3

Expert Comment

by:bryan7
Comment Utility
RXLib www.rxlib.ru (or .com) has a great component to do that on a INI or Registry: TFormPlacement I think, check it
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:chengjian
Comment Utility
Greatly thanx
First I do not want save propertises to exe.
Registry and Ini are acceptable.
but the problem is:
I do not want to save the propertises one by one. I want an easy and common way.
I have used either readcomponent and write component, it works, but, it saves the entire classes besides the propertises
if I have a button on form
when I read another button out, it should be two buttons:(

forgive my poor language
0
 
LVL 17

Expert Comment

by:inthe
Comment Utility
maybe this example from torrys is a help:

http://torry.spang.org/samples/samples/saveprop.zip
0
 
LVL 3

Expert Comment

by:bryan7
Comment Utility
"
                    I do not want to save the propertises one by one. I want an easy and common way.
                    I have used either readcomponent and write component, it works, but, it saves the entire
                    classes besides the propertises
                    if I have a button on form
                    when I read another button out, it should be two buttons:( "

hmm, sorry if I'm a bit short in mind. .I don't understand at all what's the prob.. the component can save all the properties you want from any components you want..
0
 
LVL 1

Author Comment

by:chengjian
Comment Utility
Ok, an example save component's property with both readcomponent and writecomponent would acceptable.
please send to:cjcjc@citiz.net
thanx a lot
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
Comment Utility
procedure TForm1.Button1Click(Sender: TObject);
var
  Strm : TFileStream;
begin
  Strm := TFileStream.Create('Label1.dat', fmCreate);
  with Strm do
  try
    WriteComponent(Label1);
  finally
    Free;
  end;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var
  Strm : TFileStream;
begin
  if not FileExists('Label1.dat') then Exit;
  Strm := TFileStream.Create('Label1.dat', fmOpenRead);
  with Strm do
  try
    ReadComponent(Label1);
  finally
    Free;
  end;
end;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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 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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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