Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to add controls to a form at runtime and stream the result to a file Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Files Operation
Language
Delphi 2.x
Views
74
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Is it possible to add controls to a form at runtime, then stream the result to 
single file, thus preserving all changes for next application run?

Answer:

Yes, with some caveats. You need to register all control classes that are used on 
the form plus all that the user may add at runtime. Here is a little example:

1   unit Unit1;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
7   StdCtrls;
8   
9   type
10    TForm1 = class(TForm)
11      Button1: TButton;
12      Button2: TButton;
13      procedure Button1Click(Sender: TObject);
14      procedure Button2Click(Sender: TObject);
15      procedure FormCreate(Sender: TObject);
16    private
17      { Private declarations }
18    public
19      { Public declarations }
20    end;
21  
22  var
23    Form1: TForm1;
24  
25  implementation
26  
27  {$R *.DFM}
28  
29  const
30    formdata = 'formdata.bin';
31  
32  procedure TForm1.Button1Click(Sender: TObject);
33  begin
34    with TEdit.Create(self) do
35    begin
36      left := button1.Left + button1.width + 10;
37      top := button1.top;
38      Name := 'NewEdit';
39      parent := self;
40    end;
41  end;
42  
43  procedure TForm1.Button2Click(Sender: TObject);
44  var
45    fs: TFileStream;
46  begin
47    fs := TFileStream.Create(ExtractFilePath(ParamStr(0)) + formdata, fmCreate);
48    try
49      fs.WriteComponent(self);
50    finally
51      fs.free
52    end;
53  end;
54  
55  procedure TForm1.FormCreate(Sender: TObject);
56  var
57    filename: string;
58    fs: TFileStream;
59    i: integer;
60  begin
61    filename := ExtractFilePath(ParamStr(0)) + formdata;
62    if fileExists(filename) then
63    begin
64      for i := ComponentCount - 1 downto 0 do
65        Components[i].Free;
66      fs := Tfilestream.create(filename, fmOpenRead or fmShareDenyWrite);
67      try
68        fs.ReadComponent(self);
69        button1.enabled := false;
70      finally
71        fs.free
72      end;
73    end;
74  end;
75  
76  initialization
77    RegisterClasses([TButton, TEdit]);
78  end.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC