Board index » delphi » : Create Object on the Fly?

: Create Object on the Fly?

Hello there,

I was experimenting Delphi object creation at the run-time.  I chose the
simple TButton object.  However, I was not able make the button visialbe
on the form.  Can somebody give me a clue on the cause of the prblem?

The basic code is as follows:

...
var Button2 : TButton;
begin
  Button2 := TButton.Create(Form1);
  with Button2 do
  begin
    Default := True;
    Top := Form1.Top;
    Left := Form.Left;
    Caption := 'OK';
    Height := 25;
    Width := 75;
    Enabled := True;
    Visible :=  True;
    Show;   // redundant
  end;
end

Thanks in advance for help.

regards

robert

 

Re:: Create Object on the Fly?


In article <56vrlk$...@borg.svpal.org>,
   apo...@svpal.svpal.org (Robert Lin) wrote:

Quote
>Hello there,

>I was experimenting Delphi object creation at the run-time.  I chose the
>simple TButton object.  However, I was not able make the button visialbe
>on the form.  Can somebody give me a clue on the cause of the prblem?

>The basic code is as follows:

>....
>var Button2 : TButton;
>begin
>  Button2 := TButton.Create(Form1);
>  with Button2 do
>  begin
>    Default := True;
>    Top := Form1.Top;
>    Left := Form.Left;
>    Caption := 'OK';
>    Height := 25;
>    Width := 75;
>    Enabled := True;
>    Visible :=  True;
>    Show;   // redundant
>  end;
>end

Easy.  Add 'Parent := Form1'.  The TButton.Create(Form1) set the OWNER of
the button (the object responsible for destroying the button when it's
done), but the PARENT of the button is what the button *visually* belongs
to.  For example, if you had a TPanel in there somewhere, you would still
create the button with .Create(Form1) but you'd set the Parent to Panel1.

Okey-dokey? :)

  --=- Ritchie

Re:: Create Object on the Fly?


Quote
Robert Lin wrote:

> Hello there,

> I was experimenting Delphi object creation at the run-time.  I chose the
> simple TButton object.  However, I was not able make the button visialbe
> on the form.  Can somebody give me a clue on the cause of the prblem?

> The basic code is as follows:

> ...
> var Button2 : TButton;
> begin
>   Button2 := TButton.Create(Form1);
>   with Button2 do
>   begin
>     Default := True;
>     Top := Form1.Top;
>     Left := Form.Left;
>     Caption := 'OK';
>     Height := 25;
>     Width := 75;
>     Enabled := True;
>     Visible :=  True;
>     Show;   // redundant
>   end;
> end

> Thanks in advance for help.

> regards

> robert

Try setting it's parent property.
   Parent := Form1;

I think this will do it for you.

Rick Wright

Re:: Create Object on the Fly?


You will need to set the Parent property:

  Button2.Parent:= Form1;

Remember that the Form1.Top and Form1.Left properties are in screen
coordinates. To place the button at the forms TopLeft corner just
set Button2.Top:= 0; and Button2.Left:= 0;

Hi Peter

Quote
Robert Lin wrote:

> Hello there,

> I was experimenting Delphi object creation at the run-time.  I chose the
> simple TButton object.  However, I was not able make the button visialbe
> on the form.  Can somebody give me a clue on the cause of the prblem?

> The basic code is as follows:

> ...
> var Button2 : TButton;
> begin
>   Button2 := TButton.Create(Form1);
>   with Button2 do
>   begin
>     Default := True;
>     Top := Form1.Top;
>     Left := Form.Left;
>     Caption := 'OK';
>     Height := 25;
>     Width := 75;
>     Enabled := True;
>     Visible :=  True;
>     Show;   // redundant
>   end;
> end

> Thanks in advance for help.

> regards

> robert

Re:: Create Object on the Fly?


Put in :

  Button2 := TButton.Create(Form1);
  Button2.Parent := Form1;
  etc...
  etc...

have fun,

andy

Robert Lin <apo...@svpal.svpal.org> wrote in article
<56vrlk$...@borg.svpal.org>...

Quote
> Hello there,

> I was experimenting Delphi object creation at the run-time.  I chose the
> simple TButton object.  However, I was not able make the button visialbe
> on the form.  Can somebody give me a clue on the cause of the prblem?

> The basic code is as follows:

> ...

<<< SNIP >>>>>

Other Threads