Recent

Author Topic: Advice on coding  (Read 1205 times)

petevick

  • Sr. Member
  • ****
  • Posts: 427
Advice on coding
« on: December 25, 2023, 10:24:55 am »
In the partial code below, I use FindComponent() to get the different objects whose names all share the objName string. The code works well with no noticeable lag. But I was wondering if there would be any benefit to passing the actual object names to the ShowPreview() procedure, so there would five parameters instead of one. I can't see that speed is an issue, but would it make for better or more efficient code ??

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ShowPreview(objName: String);
  2. var
  3.   aPnt: TPoint;
  4.   But: TBCButtonFocus; Combo: TBCComboBox;
  5.   Chk1: TCheckBox; Chk2: TCheckBox; GrpBox: TGroupBox;
  6. begin
  7.     But:= self.FindComponent(objName+'Norm') as TBCButtonFocus;
  8.     Combo:= self.FindComponent(objName+'Height') as TBCComboBox;
  9.     Chk1:= self.FindComponent(objName+'Bold') as TCheckBox;
  10.     Chk2:= self.FindComponent(objName+'Italic') as TCheckBox;
  11.     GrpBox:= self.FindComponent('FO'+objName) as TGroupBox;
  12.  
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12199
  • Debugger - SynEdit - and more
    • wiki
Re: Advice on coding
« Reply #1 on: December 25, 2023, 10:41:36 am »
If you have repeated groups of the same components, then you could make them a TFrame with the components in it.

Then you pass the name of the frame (or if you have: "AFrame: TMyFrame").
And you can access the components on it via
// AFrame := FindComponent(...)
AFrame.ButtonNorm;
....



If not all components are needed in each group, you can change visibility.

You can also have some of the event code on the frame, or add your own methods to the frame, and call them from your form....

petevick

  • Sr. Member
  • ****
  • Posts: 427
Re: Advice on coding
« Reply #2 on: December 25, 2023, 10:51:34 am »
If you have repeated groups of the same components, then you could make them a TFrame with the components in it.
....
I've not come across TFrame before. It looks like something I ought to read up on.
Thanks for the reply Martin.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Windows 10, Lazarus 3.2, FPC 3.2.2

cdbc

  • Hero Member
  • *****
  • Posts: 2677
    • http://www.cdbc.dk
Re: Advice on coding
« Reply #3 on: December 25, 2023, 11:09:39 am »
Hi
As Martin said, TFrame is a very nifty and helpful tool to work with.
Be careful though, with Tframe and object inspector, ie.: designtime...
I have gotten the best results with creating frames dynamically at runtime.
It's ok to design the frame visually, but don't put it on your form via the OI.
Frames work great with TPageControl and TTabSheet
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

MarkMLl

  • Hero Member
  • *****
  • Posts: 8550
Re: Advice on coding
« Reply #4 on: December 25, 2023, 12:23:53 pm »
Hi
As Martin said, TFrame is a very nifty and helpful tool to work with.
Be careful though, with Tframe and object inspector, ie.: designtime...
I have gotten the best results with creating frames dynamically at runtime.

Yes, agreed. Onto a tabbed notebook page or whatever.

Quote
It's ok to design the frame visually, but don't put it on your form via the OI.

IME, you can get away with putting a frame onto a form using the IDE.

But once you've done that, any subsequent attempt to modify the instantiated frame using the IDE will end up with a situation where you need to open the .lfm as text to clean things up.

It's been discussed before: the IDE should treat the content of an instantiated frame as read-only, and for complex reasons doesn't.

In addition, any changes to the designed frame- call it a template, pattern or whatever- will not necessarily affect the instantiated one.

Hence the doctrine that

Quote
Frames work great with TPageControl and TTabSheet

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Joanna

  • Hero Member
  • *****
  • Posts: 1419
Re: Advice on coding
« Reply #5 on: December 25, 2023, 01:23:28 pm »
It’s ok to add frame at design time if you never ever will make any changes to it. If you change original it causes all the frame instances to crash badly because they no longer match the frame class and yes fixing it will involve deleting stuff from lfm files.
Also I’d like to add that controls on frames should always have their events assigned at runtime. Do not use the object inspector to create the events it can cause trouble for other instances of the frame.

 

TinyPortal © 2005-2018