Board index » delphi » Q: Creating controls at runtime.

Q: Creating controls at runtime.

Hi all

Quick question, I want to create a control (eg a button) at runtime,
this is what I've written

procedure TfrmScreen.Add_Button;
var button: TBitBtn;
begin

     button := TBitBtn.Create(self);

     button.Caption := 'test';

     button.Top := 100;
     button.Left := 100;
     button.width := 100;
     button.height := 100;

     button.visible := true;

     button.show;

end;

But it doesn't work. Any Ideas?

I will need to create an arbitary number of these controls at runtime.

Tim

 

Re:Q: Creating controls at runtime.


Quote

>      button := TBitBtn.Create(self);

===================
        add the following here :
        button.Parent := Self;
==================
Quote
>      button.Caption := 'test';

>      button.Top := 100;

Re:Q: Creating controls at runtime.


Tim

You dont need the visible and you dont need the show, what you need is the
parent
with ctrl do
        Parent := frmScreen

Also look at SetBounds in the help for ctrl positioning.

Good Luck

--
Stuart Wheeldon
Senior Consultant
DALEC Consultants
dav...@dalec.com.au

Tim Fulcher <t...@srvnac3.nac.ac.za> wrote in article
<863710471.138...@dull.nac.ac.za>...

Quote
> Hi all

> Quick question, I want to create a control (eg a button) at runtime,
> this is what I've written

> procedure TfrmScreen.Add_Button;
> var button: TBitBtn;
> begin

>      button := TBitBtn.Create(self);

>      button.Caption := 'test';

>      button.Top := 100;
>      button.Left := 100;
>      button.width := 100;
>      button.height := 100;

>      button.visible := true;

>      button.show;

> end;

> But it doesn't work. Any Ideas?

> I will need to create an arbitary number of these controls at runtime.

> Tim

Re:Q: Creating controls at runtime.


Tim,
  You need to declare the parent property of the BitBtn.  Try:

procedure TfrmScreen.Add_Button;
var button: TBitBtn;
begin

     button := TBitBtn.Create(self);
     button.parent := Form1;

     button.Caption := 'test';

     button.Top := 100;
     button.Left := 100;
     button.width := 100;
     button.height := 100;

     button.visible := true;

end;

Also, you could use SetBounds instead of setting the Top, Left, Width and
Height individually.

   button.SetBounds(100,100,100,100);

Look it up in the online help.

Hope this helps!
--
Rod Geraghty (:>)
GERA-Tech
Ottawa, Canada
gera...@ibm.net

Tim Fulcher <t...@srvnac3.nac.ac.za> wrote in article
<863710471.138...@dull.nac.ac.za>...

Quote
> Hi all

> Quick question, I want to create a control (eg a button) at runtime,
> this is what I've written

> procedure TfrmScreen.Add_Button;
> var button: TBitBtn;
> begin

>      button := TBitBtn.Create(self);

>      button.Caption := 'test';

>      button.Top := 100;
>      button.Left := 100;
>      button.width := 100;
>      button.height := 100;

>      button.visible := true;

>      button.show;

> end;

> But it doesn't work. Any Ideas?

> I will need to create an arbitary number of these controls at runtime.

> Tim

Re:Q: Creating controls at runtime.


Tim Fulcher <t...@srvnac3.nac.ac.za> wrote in article
<863710471.138...@dull.nac.ac.za>...

Quote
> Hi all

> Quick question, I want to create a control (eg a button) at runtime,
> this is what I've written

> procedure TfrmScreen.Add_Button;
> var button: TBitBtn;
> begin

>      button := TBitBtn.Create(self);

Try
        Form.Insertcontrol (button);
Quote
>      button.Caption := 'test';

>      button.Top := 100;
>      button.Left := 100;
>      button.width := 100;
>      button.height := 100;

>      button.visible := true;

>      button.show;

> end;

> But it doesn't work. Any Ideas?

> I will need to create an arbitary number of these controls at runtime.

> Tim

Other Threads