INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Dynamic runtime complex component creation

Dynamic runtime complex component creation

(OP)
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[i] := TMyIconObject.Create(Form1);
MyIcons[i].Width := 50;
MyIcons[i].Height := 50;
MyIcons[i].Position.X := 50 + (75 * i);
MyIcons[i].Position.Y := 100;

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

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

End;

end;

end.

RE: Dynamic runtime complex component creation

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[i] := TMyIconObject.Create(Form1);
MyIcons[i].Width := 50;
MyIcons[i].Height := 50;
MyIcons[i].Position.X := 50 + (75 * i);
MyIcons[i].Position.Y := 100;
MyIcons[i].IconRectangle := TMyIconRectangle.Create(MyIcons[i]);
MyIcons[i].IconRectangle.Parent := MyIcons[i];

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

MyIcons[i].IconLabel:= TMyIconLabel.Create(MyIcons[i]);
MyIcons[i].IconLabel.Parent := MyIcons[i];

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

RE: Dynamic runtime complex component creation

(OP)
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.


RE: Dynamic runtime complex component creation

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close