I can create a form and then apply create some components on it
using a dfm file as a template. For example I can do this :
procedure TForm1.Button1Click(Sender: TObject);
var
DfmTxt : TStringStream;
Dfm: TMemoryStream;
Sl : TStringList;
ST : string;
Form : TForm2;
begin
Sl:=TStringList.Create;
Sl.LoadFromFile('unit3.dfm');
ST:=Sl.Text;
Sl.Free;
Form:=TForm2.Create(Application);
DfmTxt:=TStringStream.Create(ST);
DfmTxt.Position:=0;
Dfm := TMemoryStream.Create;
ObjectTextToBinary(DfmTxt, Dfm);
Dfm.Position := 0;
Form:=Dfm.ReadComponent(Form) as TForm2;
Form.ShowModal;
Form.Free;
end;
This works Ok.
But I want to be able to create new componets, edit name, positons
and property of the components and then save them to a dfm file again.
In the next time the user run the program it will use the updated dfm
file.
But if I want do a :
procedure TForm2.Button2Click(Sender: TObject);
begin
...
Dfm.WriteComponent(Self);
Dfm.Position:=0;
ObjectBinaryToText(Dfm,DfmTxt);
Sl.SaveToFile('unit3.dfm');
...
end;
On the resulting form and then save it to a dfm file it does not
have the same componets on it.
The problem is this :
- On this example I have a main form (form1) and a base form to the
forms I want to create at runtime. It is called form2. The form 2 has
a tlabel (labe2) on it. To create form3.dfm I did create in the Delphi
IDE a new form (form3) inherited from form2. And then I put a label
(label2) on it. The form3.dfm is like this :
inherited Form3: TForm3
Caption = 'Form3'
OldCreateOrder = True
PixelsPerInch = 96
TextHeight = 13
object Label2: TLabel [1]
Left = 32
Top = 88
Width = 32
Height = 13
Caption = 'Label2'
end
end
- But when I do a WriteComponent and ObjectBinaryToText to save the
form it write a file
like this :
object Form3: TForm2
Left = 231
Top = 103
Width = 696
Height = 480
ActiveControl = Button2
Caption = 'Form3'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
Visible = True
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 24
Top = 40
Width = 32
Height = 13
Caption = 'Label1'
end
object Label2: TLabel
Left = 32
Top = 88
Width = 32
Height = 13
Caption = 'Label2'
end
end
- This is the problem. WriteComponent write all components on the
form I have in memory,
not just the components I add on form3. Even the components in form2
like label1. But I want
it to write just the componets from added on form3 or created at
runtime.
Are there some solution to my problem ?
Danilo.
Instead of WriteComponent, try using WriteDescendent and pass it the
ancestor form.
HTH
TOndrej