Board index » delphi » Dynamically creating components on a form

Dynamically creating components on a form

I saw a message about a week ago that did this... but of course I did not
save it and I cannot seem to get this to work.

How do you create components programmatically and show them on a form.  For
example,  I would want to add a certain amount of checkboxes to a form
depending on a certain value.  I tried this code to add one check box to a
form:

procedure TForm1.Button1Click(Sender: TObject);
var
  chk : TCheckbox;
begin
    chk  := TCheckbox.create(Form1);
    chk.top := 10;
    chk.left := 10;
    chk.show;
    chk.refresh;
    chk.repaint;
    form1.refresh;
end;

I cannot get the checkbox to be visible.  That is why I added the show,
refresh, repaint methods after creating it.

What am I missing?

Thanks!

 

Re:Dynamically creating components on a form


Matt,

You are missing the Parent property of your control.

The parent of a control tells it which component (like a form or a panel)
contains your new one, and therefore where it should paint itself.

You do not need to Show the component or make it Visible or anything
else...  just set its left and top like you have already done and then set
its parent...

Then for fun, put a button on a form to dynamically change the parent ffrom
one panel to another and your component will look like it is moving around
on your form.

I am not sure if this question is in a FAQ, but it is asked often enough,
it probably is.

Have fun...

Ken
--
[email protected]
[email protected]
Notice at no time do my fingers leave my hands...

Matt Peebles <[email protected]> wrote in article
<[email protected]>...

Quote
> I saw a message about a week ago that did this... but of course I did not
> save it and I cannot seem to get this to work.

> How do you create components programmatically and show them on a form.
For
> example,  I would want to add a certain amount of checkboxes to a form
> depending on a certain value.  I tried this code to add one check box to
a
> form:

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   chk : TCheckbox;
> begin
>     chk  := TCheckbox.create(Form1);
>     chk.top := 10;
>     chk.left := 10;
>     chk.show;
>     chk.refresh;
>     chk.repaint;
>     form1.refresh;
> end;

> I cannot get the checkbox to be visible.  That is why I added the show,
> refresh, repaint methods after creating it.

> What am I missing?

> Thanks!

Re:Dynamically creating components on a form


When I do this on a dynamically created form, though, the component still
does not show.  I would use the OnCreate method for the form to
dynamically create the component (a TDBGrid descendant in this case) but
it will not show.

Regards
Glen

Kenneth A. Faw ([email protected]) wrote:
: Matt,

: You are missing the Parent property of your control.

: The parent of a control tells it which component (like a form or a panel)
: contains your new one, and therefore where it should paint itself.

: You do not need to Show the component or make it Visible or anything
: else...  just set its left and top like you have already done and then set
: its parent...

: Then for fun, put a button on a form to dynamically change the parent ffrom
: one panel to another and your component will look like it is moving around
: on your form.

: I am not sure if this question is in a FAQ, but it is asked often enough,
: it probably is.

: Have fun...

: Ken
: --
: [email protected]
: [email protected]
: Notice at no time do my fingers leave my hands...

: Matt Peebles <[email protected]> wrote in article
: <[email protected]>...
: > I saw a message about a week ago that did this... but of course I did not
: > save it and I cannot seem to get this to work.
: >
: > How do you create components programmatically and show them on a form.
: For
: > example,  I would want to add a certain amount of checkboxes to a form
: > depending on a certain value.  I tried this code to add one check box to
: a
: > form:
: >
: > procedure TForm1.Button1Click(Sender: TObject);
: > var
: >   chk : TCheckbox;
: > begin
: >     chk  := TCheckbox.create(Form1);
: >     chk.top := 10;
: >     chk.left := 10;
: >     chk.show;
: >     chk.refresh;
: >     chk.repaint;
: >     form1.refresh;
: > end;
: >
: > I cannot get the checkbox to be visible.  That is why I added the show,
: > refresh, repaint methods after creating it.
: >
: > What am I missing?
: >
: > Thanks!
: >

Re:Dynamically creating components on a form


Matt,

You have to give the component a parent.

chk.parent:=form1;

Fanie Marais
[email protected]

Quote
"Matt Peebles" <[email protected]> writes: > I saw a message about a week ago that did this... but of course I did not
> save it and I cannot seem to get this to work.

> How do you create components programmatically and show them on a form.  For
> example,  I would want to add a certain amount of checkboxes to a form
> depending on a certain value.  I tried this code to add one check box to a
> form:

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   chk : TCheckbox;
> begin
>     chk  := TCheckbox.create(Form1);
>     chk.top := 10;
>     chk.left := 10;
>     chk.show;
>     chk.refresh;
>     chk.repaint;
>     form1.refresh;
> end;

> I cannot get the checkbox to be visible.  That is why I added the show,
> refresh, repaint methods after creating it.

> What am I missing?

> Thanks!

Re:Dynamically creating components on a form


Matt Peebles,[email protected],UseNet (m) wrote at 17:11 on 20.12.96
to comp.delphi.misc about "Dynamically creating components on a form":
-----------------------------

Quote
>I saw a message about a week ago that did this... but of course I did not
>save it and I cannot seem to get this to work.

>How do you create components programmatically and show them on a form.  For
>example,  I would want to add a certain amount of checkboxes to a form
>depending on a certain value.  I tried this code to add one check box to a
>form:

>procedure TForm1.Button1Click(Sender: TObject);
>var
>  chk : TCheckbox;
>begin
>    chk  := TCheckbox.create(Form1);
>    chk.top := 10;
>    chk.left := 10;
>    chk.show;
>    chk.refresh;
>    chk.repaint;
>    form1.refresh;
>end;

>I cannot get the checkbox to be visible.  That is why I added the show,
>refresh, repaint methods after creating it.

>What am I missing?

I think, you have to set the parent - Property of the new checkbox in the
following way !

chk.parent := Form1;

Bye and have a nice xmas
Peter

- Show quoted text -

Quote

>Thanks!

>--- Internet Message Header Follows ---
>Path:
>sun.magnet.at!Austria.EU.net!EU.net!howland.erols.net!www.nntp.primenet.
>om!nntp.primenet.com!mr.net!nntp.earthlink.net!usenet
>From: "Matt Peebles" <[email protected]>
>Newsgroups: comp.lang.pascal.delphi.misc
>Subject: Dynamically creating components on a form
>Date: 20 Dec 1996 16:11:58 GMT
>Organization: MIDS, Inc.
>Lines: 27
>Message-ID: <[email protected]>
>NNTP-Posting-Host: max2-lg-ca-01.earthlink.net
>X-Newsreader: Microsoft Internet News 4.70.1155

-----------------------------
Peter Havel
--- OffRoad 1.9s registered to Peter Havel

Re:Dynamically creating components on a form


procedure TForm1.Button1Click(Sender: TObject);
var
   oChk : TCheckBox;
begin
   oChk         := TCheckBox.Create( Self );
   oChk.Top     := 10;
   oChk.Left    := 10;
   oChk.Caption := 'Hello world!';
   InsertControl( oChk );
end;

regards,

Other Threads