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 controls at run time
Mon, Feb 5 2018 4:15 AMPermanent Link

Paul Coshott

Avatar

Hi All,

I have a form where the user can create TPanel's within a TScrollPanel. If the user double clicks the caption bar, I need an event to run, that changes the caption bars background colour, so the user can see which TPanel is selected. I can link up the event, but I'm not sure how to reference the control.

The following code with the CaptionBarDblClick event procedure does not work. How should I do this?

One other question : is it not possible to register when the user single clicks the caption bar?

Cheers,
Paul

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

procedure TfSchedule.panelDayCaptionBarDblClick(Sender: TObject);
begin
 with Sender as TPanel do begin
   CaptionBar.Background.Fill.Color := clRed;
 end;
end;

procedure TfSchedule.Button7Click(Sender: TObject);
begin
 ScrollPanel1.BeginUpdate;
 try
   with TPanel.Create(ScrollPanel1) do begin
     Layout.Position := lpTopLeft;
     Layout.Stretch := lsBottom;
     Layout.Consumption := lcRight;
     Background.Fill.Color := clLightSkyBlue;
     CaptionBar.Height := 25;
     CaptionBar.Background.Fill.Color := clElevateGray;
     CaptionBar.Caption := 'Panel ' + IntToStr(ScrollPanel1.ControlCount);
     CaptionBar.AllowClose := False;
     CaptionBar.AllowMinimize := False;
     CaptionBar.AllowMove := False;
     Width := 300;
     OnCaptionBarDblClick := panelDayCaptionBarDblClick;
   end;
 finally
   ScrollPanel1.EndUpdate;
 end;
end;
Mon, Feb 5 2018 5:15 AMPermanent Link

Matthew Jones

Paul Coshott wrote:

> The following code with the CaptionBarDblClick event procedure does not work. How should I do this?

It looks like it should work to me - and I've done a lot of this sort of adding a panel and wiring up the events. Two things I'd do here, and one is to use my "DebugReport" function which outputs the text to a memo so I can see what is happening. The memo is right aligned to the form and normally hidden but can be shown by double-clicking on a secret place (and actually only if you have set a value in storage, but that's getting fancy). Anyway, it is just the age old "printf" debugging, but may tell you what is happening. Is the event called at all?

The other option is to use the browser debugging tools, and put breakpoints on the code to see what happens. I often find it reveals all sorts of interesting things about why my assumptions were wrong.

And of course finally, just a simple sanity check - drop on a panel, add the event and check it works as you expect when not done in code.

--

Matthew Jones
Mon, Feb 5 2018 8:02 AMPermanent Link

Paul Coshott

Avatar

"Matthew Jones" wrote:

> It looks like it should work to me - and I've done a lot of this sort of adding a panel and wiring up the events.

Hi Matthew,

Thanks for the reply.
It's actually not even compiling. If I have:

with Sender as TPanel do begin

the error I get is: [Error] Templates.wbs (143,15): Expected do but instead found as

If I try:

with Sender do begin
   CaptionBar.Background.Fill.Color := clRed;

the error is : [Error] Templates.wbs (144,5): The referenced variable, parameter, or function CaptionBar does not exist

Any ideas?

Thanks,
Paul
Mon, Feb 5 2018 8:49 AMPermanent Link

Uli Becker

Pau,

with Sender do begin
   CaptionBar.Background.Fill.Color := clRed;

Use

with TPanel(Sender)  do begin
...

Uli
Mon, Feb 5 2018 8:56 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> Use
>
> with TPanel(Sender)  do begin

Or better yet, ditch that horrible "with", and use:

   TPanel(Sender).CaptionBar.Background.Fill.Color := clRed;

If I'm doing multiple operations on it, I assign to a local variable and use that. This has the advantage that the Chrome debugger shows me the values etc. Perhaps EWB creates a suitable temporary when you use "with", but it won't be the name you would choose and recognise, and it is still horrible. 8-)

--

Matthew Jones
Mon, Feb 5 2018 2:08 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Matthew,

<< If I'm doing multiple operations on it, I assign to a local variable and use that. This has the advantage that the Chrome debugger shows me the values etc. Perhaps EWB creates a suitable temporary when you use "with", but it won't be the name you would choose and recognise, and it is still horrible. 8-) >>

It does, but only with its internal runtime.  For the browser, it leaves the with handling up to the V8 engine.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Feb 5 2018 9:26 PMPermanent Link

Paul Coshott

Avatar

Awesome, thanks guys.

Cheers,
Paul
Image