Board index » delphi » Create an object in an created object

Create an object in an created object

Hi all,

What I'm trying to say is how can I create an object in another created object
on runtime.

For instance,  I create an Groupbox  and in that object I like to create,
let's say, a Label.

The problem by creating that Label is that you need to set the parent to
Self.Groupbox.Name. The compilererror I'm getting, verry logical btw, is
"Undeclaired identifier". This object is not created until runtime.

My question is how can I in desingcode say that the label must be put on the
groupbox??

If anyone could give me some example code about this.

Thanks in advance.

Regards Raymond.

-----------------------------------------------------------------
Raymond Mul                                  System Administrator
Warandelaan 2,  P.O. Box 90153,  5000 LE Tilburg, The Netherlands
E-mail: R.L.J....@Kub.nl        Home-Page http://cwis.kub.nl/~iva
IVA Tilburg, institute for social policy research and consultancy
-----------------------------------------------------------------

 

Re:Create an object in an created object


Quote
Raymond Mul wrote:

> Hi all,

> What I'm trying to say is how can I create an object in another created object
> on runtime.

> For instance,  I create an Groupbox  and in that object I like to create,
> let's say, a Label.

> The problem by creating that Label is that you need to set the parent to
> Self.Groupbox.Name. The compilererror I'm getting, verry logical btw, is
> "Undeclaired identifier". This object is not created until runtime.

> My question is how can I in desingcode say that the label must be put on the
> groupbox??

> If anyone could give me some example code about this.

> Thanks in advance.

> Regards Raymond.

I hadn't done this sort of thing before, so I tried it, for my own
education.

You do need to declare the identifiers of course; I did it in the var
section of the unit:

oGroupBox : TGroupBox;
oLabel : TLabel

Then in code (say in a click event for a button on the form), something
like:

  oGroupBox := TGroupBox.Create(self);
  oGroupBox.Top := 100;
  oGroupBox.Left := 100;
  oGroupBox.Height := 500;
  oGroupBox.Width := 500;
  oGroupBox.Parent := self;
  oLabel := TLabel.Create(self);
  oLabel.Top := 100;
  oLabel.Left := 100;
  oLabel.Height := 100;
  oLabel.Width := 200;
  oLabel.Caption := 'Hello, folks!';
  oLabel.Parent := oGroupBox;

Notice it's not the Name property of the parent object you set into
.Parent; it's the object itself.

By the way, in Delphi 2, if I declare oGroupBox and oLabel as members of
my form object, I get a message at compile time asking if I want to get
rid of them (because they're not known to the IDE, I guess).  Life's
easier if you simply make them variables in the unit.

Re:Create an object in an created object


On Fri, 14 Nov 1997 14:50:46 GMT, R.L.J.Mul@|No_Spam|KUB.NL (Raymond

Quote
Mul) wrote:

>Hi all,

>What I'm trying to say is how can I create an object in another created object
>on runtime.

>For instance,  I create an Groupbox  and in that object I like to create,
>let's say, a Label.

>The problem by creating that Label is that you need to set the parent to
>Self.Groupbox.Name. The compilererror I'm getting, verry logical btw, is
>"Undeclaired identifier". This object is not created until runtime.

>My question is how can I in desingcode say that the label must be put on the
>groupbox??

>If anyone could give me some example code about this.

>Thanks in advance.

>Regards Raymond.

procedure tmainform.formcreate;
var
  gb : tGroupbox;
  label : tLabel;
begin
  gb:=tGroupbox.create (self);
  gb.parent:=self;
  gb.setbounds (0,0,100,100);
  label:=tlabel.create (gb);
  label.parent:=gb;
  label.autosize:=true;
  label.caption:='blah';
end;

if you want to access the label/groupbox from another procedure in the
form, just put the variable decl's in your form's private section.

Other Threads