Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Creating a control at run time with another control just created
Mon, Feb 5 2018 10:41 PMPermanent Link

Paul Coshott

Avatar

Hi All,

I am creating a TBasicPanel at run time, and then I want to create a TLabel within the TBasicPanel, but I'm not sure how to reference the parent control (the newly created TBasicPanel) when creating the TLabel.

Thanks,
Paul

-----------------------------------------------------------------------------------------

procedure TfSchedule.Button7Click(Sender: TObject);
var
 sBPName : string;
begin
 ScrollPanel1.BeginUpdate;
 try
   with TBasicPanel.Create(ScrollPanel1) do begin
     iComp := iComp + 1;
     sBPName := 'panDay' + IntToStr(iComp);
     Name := sBPName;
     Layout.Position := lpTopLeft;
     Layout.Stretch := lsBottom;
     Layout.Consumption := lcRight;
     Background.Fill.Color := clLightSkyBlue;
     Width := 300;
   end;
   with TLabel.Create( ???????? ) do begin          ///// how do I reference the parent control ??
     AutoSize.Height := False;
     AutoSize.Width := False;
     Layout.Position := lpTopLeft;
     Layout.Stretch := lsRight;
     Layout.Consumption := lcBottom;
     Caption := 'Monday';
   end;
 finally
   ScrollPanel1.EndUpdate;
 end;
end;

-----------------------------------------------------------------------------------------
Mon, Feb 5 2018 11:46 PMPermanent Link

Rick

On 06/02/18 14:41, Paul Coshott wrote:
>
> I am creating a TBasicPanel at run time, and then I want to create a TLabel within the TBasicPanel, but I'm not sure how to reference the parent control (the newly created TBasicPanel) when creating the TLabel.
>
>

Probably the easiest way is to use a local variable when creating the
basic panel.

var
  bp: TBasicPanel;

try
  bp:=TBasicPanel.Create(ScrollPanel1);
  with bp do begin
  end;
  with TLabel.Create(bp) do begin
  end;
finally
end;

--
Rick
Tue, Feb 6 2018 2:09 AMPermanent Link

Paul Coshott

Avatar

wrote:

> Probably the easiest way is to use a local variable when creating the
> basic panel.

That worked perfectly. Thanks heaps.

Cheers,
Paul
Tue, Feb 6 2018 2:10 AMPermanent Link

Michael Dreher

Paul Coshott wrote:

// ... how to reference the parent control ...

Another method using nested with statements, assuming here the name of the form is 'Form1'.

procedure TForm1.Button1Click(Sender: TObject);
var
sBPName : string;
begin
  with TBasicPanel.Create(ScrollPanel1) do begin
    iComp := iComp + 1;
    sBPName := 'panDay' + IntToStr(iComp);
    Name := sBPName;
    Layout.Position := lpTopLeft;
    Layout.Stretch := lsBottom;
    Layout.Consumption := lcRight;
    Background.Fill.Color := clLightSkyBlue;
    Width := 300;
    with TLabel.Create(Form1.FindComponent(Name, true)) do begin   
      Layout.Position := lpTopLeft;
      Caption := 'Monday';
    end;
  end;
end;

M.Dreher
Tue, Feb 6 2018 2:42 AMPermanent Link

Uli Becker

Hi Paul,

just to let you know: I posted a custom component TCaptionPanel.

Uli
Tue, Feb 6 2018 3:07 AMPermanent Link

Paul Coshott

Avatar

Uli Becker wrote:

> just to let you know: I posted a custom component TCaptionPanel.

Hey Uli,

Cool, thanks. I'll check it out.

Cheers,
Paul
Tue, Feb 6 2018 4:32 AMPermanent Link

Matthew Jones

Rick wrote:

> Probably the easiest way is to use a local variable when creating the basic panel.

Indeed. ("With" bites again. 8-)

--

Matthew Jones
Image