Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic runtime complex component creation 1

Status
Not open for further replies.

Masterguba

Programmer
May 27, 2011
4
IT
What I'm trying to do is to create dynamically a component class that will contain other components.
It seems that I cannot or I do not do the creation of subcomponents correctly.
The project compiles fine, but when it runs it rises Access Violation Error.
This is the code.
What I'm doing wrong ?

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes,
System.Classes, System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms,
FMX.Dialogs, FMX.StdCtrls, FMX.Objects, FMX.Layouts;

type

TMyIconLabel = class(TLabel)
public
Constructor Create(AOwner : TComponent);
end;

TMyIconRectangle = class(TRectangle)
public
Constructor Create(AOwner : TComponent);
end;

TMyIconObject = class(TLayout)
IconLabel : TMyIconLabel;
IconRectangle : TMyIconRectangle;
private
{ Private declarations }
public
{ Public declarations }
end;

TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
MyIcons : Array[1..10] of TMyIconObject;

implementation

{$R *.fmx}

Constructor TMyIconLabel.Create(AOwner : TComponent);
Begin
inherited;
End;

Constructor TMyIconRectangle.Create(AOwner : TComponent);
Begin
inherited;
End;

procedure TForm1.FormCreate(Sender: TObject);
Var i : Integer;
begin
for i := 1 to 10 do
Begin
MyIcons := TMyIconObject.Create(Form1);
MyIcons.Width := 50;
MyIcons.Height := 50;
MyIcons.Position.X := 50 + (75 * i);
MyIcons.Position.Y := 100;

MyIcons.IconRectangle.Align := TAlignLayout.Client;
MyIcons.IconRectangle.Fill.Color := $FFFFFFFF;

MyIcons.IconLabel.Align := TAlignLayout.Client;
MyIcons.IconLabel.Text := 'Icon #' + IntToStr(i);

End;

end;

end.
 
You didn't say which line you get the error on.

You also didn't mention the version of Delphi. That could change your options/answers.

In your code you haven't created the IconRectangle and the IconLabel objects.

I haven't tested but I think you want something like:
procedure TForm1.FormCreate(Sender: TObject);
Var i : Integer;
begin
for i := 1 to 10 do
Begin
MyIcons := TMyIconObject.Create(Form1);
MyIcons.Width := 50;
MyIcons.Height := 50;
MyIcons.Position.X := 50 + (75 * i);
MyIcons.Position.Y := 100;
[highlight #FCE94F]MyIcons.IconRectangle := TMyIconRectangle.Create(MyIcons);
MyIcons.IconRectangle.Parent := MyIcons;[/highlight]
MyIcons.IconRectangle.Align := TAlignLayout.Client;
MyIcons.IconRectangle.Fill.Color := $FFFFFFFF;

[highlight #FCE94F]MyIcons.IconLabel:= TMyIconLabel.Create(MyIcons);
MyIcons.IconLabel.Parent := MyIcons;[/highlight]
MyIcons.IconLabel.Align := TAlignLayout.Client;
MyIcons.IconLabel.Text := 'Icon #' + IntToStr(i);
 
Dear DjangMan,

Thank you for your notice![thumbsup2]
Sorry for the misinformation, the version of Delphi I'm using is XE6.
Really I was forgetting to create the objects.[hammer]
Now it compiles and it runs without any error, but the problem is that nothing is visible.
Do you have an idea about why is that ?
This is a Firemonkey project. Do you know if there are some other "manoeuvres" to
do in dynamic component creation special for Firemonkey ?
Thanks again for your precious help.


 
I haven't done any Firemonkey coding so I can't be much help there. Have you tried setting .Visible to true? You might need to call .Invalidate to force a repaint of the whole object.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top