Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Iterating Controls to multiple levels
Tue, Feb 6 2018 3:18 AMPermanent Link

Paul Coshott

Avatar

Hi All,

The following code works, but I'm wondering if there is a better way to write the code. It's a bit messy just going to 2 levels, and I will need to go to at least 4, maybe 5 levels down.

The form has a scroll panel on it. Within this, there are multiple basic panels, and each basic panel could have 1 or more labels on it. One of these labels (on each basic panel) has it's layout set to be at the top of the panel. The user can click this and it gets it background set to red (to show it's selected).

The following code sets all these labels backgrounds back to elevate gray. At the moment, really just writing the code as a learning tool.

Cheers,
Paul

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

procedure TfTemplates.ResetDayHeaders;
var
 i, j : integer;
begin
 for i := 0 to ScrollPanel1.ControlCount - 1 do begin
   if ScrollPanel1.Controls[i] is TBasicPanel then begin
     if Copy(ScrollPanel1.Controls[i].Name, 1, 10) = 'bpSchedDay' then begin
       for j := 0 to TBasicPanel(ScrollPanel1.Controls[i]).ControlCount - 1 do begin
         if TBasicPanel(ScrollPanel1.Controls[j]).Controls[j] is TLabel then begin
           if Copy(TBasicPanel(ScrollPanel1.Controls[i]).Controls[j].Name, 1, 11) = 'labSchedDay' then begin
             TLabel(TBasicPanel(ScrollPanel1.Controls[i]).Controls[j]).Background.Fill.Color := clElevateGray;
           end;
         end;
       end;
     end;
   end;
 end;
end;

----------------------------------------------------------------------------------------------
Tue, Feb 6 2018 4:36 AMPermanent Link

Matthew Jones

Paul Coshott wrote:

> but I'm wondering if there is a better way to write the code

There is - create a class to contain member variables which point to the various "components" of your panel. the class create can then create the various components, labels and panels, and it then owns everything. You can then add functions or properties to set the various aspects, and it is all encapsulated in one place.

I did this successfully for a while. Since then, I've taken to making things into forms, and then parenting the forms on a placeholder panel. This encapsulates it even more, and allows easier re-use.

--

Matthew Jones
Tue, Feb 6 2018 6:08 AMPermanent Link

Mark Brooks

Slikware

Avatar

"Matthew Jones" wrote:

>> Since then, I've taken to making things into forms, and then parenting the forms on a placeholder panel. This encapsulates it even more, and allows easier re-use.

Matthew - tell me more - is this "akin" to Delphi's old Frames mechanism?
Tue, Feb 6 2018 7:24 AMPermanent Link

Uli Becker

Paul,

<<
The following code works, but I'm wondering if there is a better way to write the code. It's a bit messy just going to 2 levels, and I will need to go to at least 4, maybe 5 levels down
>i>

I do this kind of stuff quite often and prefer Matthew's first approach. In that case you would create a class which contains all controls of an "item".
Then you create an array of this class. If you create your panels and child controls at runtime, just assign a tag to each control, so that you are able to access the matching "record" in the array.

I can send you some code snippets from my latest app if you want. (see Screenshot)

Uli


Uli



Attachments: Screenshot_20180206-132041.png
Tue, Feb 6 2018 8:34 AMPermanent Link

Matthew Jones

Mark Brooks wrote:

>  is this "akin" to Delphi's old Frames mechanism?

Yes, though perhaps more akin to placing a Delphi form on a form. 8-)

 frmJobOverview := TfrmJobOverview.Create(pnlParent);

Very simple to do.

--

Matthew Jones
Tue, Feb 6 2018 8:45 AMPermanent Link

Matthew Jones

Uli Becker wrote:

> I do this kind of stuff quite often and prefer Matthew's first approach.

I'm reminded I blogged it:

http://matthew-jones.com/making-a-prettier-grid-in-elevate-webbuilder/

But the forms are better now I think - my FormList solves some of the repetition issues, but when you get into virtualising it gets complicated.

--

Matthew Jones
Tue, Feb 6 2018 10:13 AMPermanent Link

Uli Becker

Matthew,

<<
But the forms are better now I think - my FormList solves some of the repetition issues, but when you get into virtualising it gets complicated.
>>

Sounds good, I'll use it in my next project for sure.

Uli
Image