Board index » delphi » Creating VC's on the fly?

Creating VC's on the fly?

Hi!

Is there any way to create visual components at run-time?  For example,
I tried the following code, but it does not display the button:

var
  Button1: TButton; { global variable }

begin
  Button1:= TButton.Create(Form1); { or Self }
  Button1.Left:= 50;
  Button1.Top:= 50;
  Button1.Width:= 100;
  Button1.Height:= 50;

  Button1.Show;
  Button1.Refresh;
  Form1.Refresh
end;

TIA

Pieter

 

Re:Creating VC's on the fly?


Pieter A. Myburgh (piet...@iafrica.com) wrote:
: Hi!
:
: Is there any way to create visual components at run-time?  

Try this (create a new blank form, and put a button on it, and then put this
code as your buttonclick method; you must add Buttons to your USES clause);

{//////////////////////////////////////////}
procedure TForm1.Button1Click(Sender: TObject);
Var
  A:TForm;
  B:TBitBtn;
begin
  A:=Tform.Create(Application);
  With A Do Begin
    Try
      B:=TBitBtn.Create(A);
      B.Parent:=A;
      B.Kind:=bkOK;
      B.Left:=5;
      B.Top:=5;
      B:=TBitBtn.Create(A);
      B.Parent:=A;
      B.Kind:=bkCancel;
      B.Left:=5;
      B.Top:=10+B.height;
      If ShowModal=idOK Then Label1.Caption:='OK' else
        Label1.Caption:='Cancel';
    Finally
      Free;
    End;
  End;
end;
{//////////////////////////////////////////}

Dr. A{*word*73}la A. Olowofoyeku (The African Chief)
E-mail   : la...@keele.ac.uk
Author of: Chief's Installer Pro 2.80 for Win16 and Win32:
           winner of PC PLUS Magazine Gold Award (April 1995 U.K. edition)
           ftp://ftp.demon.co.uk/pub/ibmpc/win3/apps/chief/pro/cinstp28.zip
           ftp://ftp.cis.ksu.edu/pub/upload/cinstp28.zip
           ftp://ftp.coast.net/SimTel/win3/install/cinstp28.zip

Other Threads