Lazarus

Programming => LCL => Topic started by: FrankBKK on June 24, 2021, 02:46:53 pm

Title: Container for non-visual components ?
Post by: FrankBKK on June 24, 2021, 02:46:53 pm
What is the best approach to place non-visual components on a Form to avoid clutter and constant re-arranging ?

I tried to put them in a DataModule  but that created a bunch of other problems for some components.
Title: Re: Container for non-visual components ?
Post by: lucamar on June 24, 2021, 03:24:06 pm
For a relatively small number of them (say, open/save/print dialogs, main/popup menus, action list, and little else) what I do is to place a "discardable" panel aligned some convenient place and put the components over it. Then I either hide or destroy the panel on startup.

Maybe not the best solution but it works quite well most of the times if using a DataModule gives you problems.
Title: Re: Container for non-visual components ?
Post by: Zvoni on June 24, 2021, 03:50:22 pm
If (and that's a very big "if") i use something like that (e.g. the Database-Trinity - Connection, Transaction, Query) i actually resize my Form to "big", and put those components "out of the way" after i have set up everything in the Object Inspector.
Then i size the Form back to the size i want.
Done
Title: Re: Container for non-visual components ?
Post by: wp on June 24, 2021, 03:59:39 pm
With nonvisual components on a TDataModule it is important that the datamodule is created BEFORE the form which uses them. Go to the project options, section "Forms", and move the datamodule to the top of the "Auto-create forms". I do this regularly with imagelists shared between several forms. See also the lpr project file:
Code: Pascal  [Select][+][-]
  1. begin
  2.   RequireDerivedFormResource:=True;
  3.   Application.Title:='project1';
  4.   Application.Scaled:=True;
  5.   Application.Initialize;
  6.   Application.CreateForm(TDataModule1, DataModule1);  // <--- Must be BEFORE Form1
  7.   Application.CreateForm(TForm1, Form1);
  8.   Application.Run;
  9. end.

You can turn off non-visual components altogether by unchecking the option "Show nonvisual components" in the form's context menu. Then you can find the non-visual components only in the object tree above the object inspector.

In many cases I populate most properties of the nonvisual components at runtime. In this case it does not make much sense to put them on the form at designtime at all. Create them at runtime - it's only a single line:
Code: Pascal  [Select][+][-]
  1.   SaveDialog1 := TSaveDialog.Create(self);
In this context it is a good trick to temporarily drop the component on the form and delete it immediately afterwards - this adds the required unit(s) to the uses clause.
Title: Re: Container for non-visual components ?
Post by: FrankBKK on June 24, 2021, 06:03:18 pm
With nonvisual components on a TDataModule it is important that the datamodule is created BEFORE the form which uses them.

That is part of my problem - I have some non-visual components that refer to components on the main form (e.g. LazReport for StringGrids) so I run into a chcken-egg problem. 


In many cases I populate most properties of the nonvisual components at runtime. In this case it does not make much sense to put them on the form at designtime at all. Create them at runtime - it's only a single line:

Agreed - I think that's the way to go ...

Thanks for your help !
Title: Re: Container for non-visual components ?
Post by: Remy Lebeau on June 25, 2021, 03:44:43 am
That is part of my problem - I have some non-visual components that refer to components on the main form (e.g. LazReport for StringGrids) so I run into a chcken-egg problem. 

You could simply use multiple DataModules.

Place any non-visual controls that the Form requires into a DataModule that is created before the Form is created.

Place any non-visual controls that require the Form into another DataModule that is created after the Form is created.
Title: Re: Container for non-visual components ?
Post by: egsuh on June 25, 2021, 03:59:14 am
WOW I found several approaches here.

Quote
In this context it is a good trick to temporarily drop the component on the form and delete it immediately afterwards - this adds the required unit(s) to the uses clause.

I drop a component on a form, then I move the definitions of non-visual components from published to private (or public) section, then I delete component from the form. This leaves declarations of the classes.

Quote
Place any non-visual controls that the Form requires into a DataModule that is created before the Form is created.

If the non-visual controls in the datamodule are used only by the form, then datamodule may be created within form creation by manual code.

Quote
You can turn off non-visual components altogether by unchecking the option "Show nonvisual components" in the form's context menu.

I did not know this. Learning a lot everyday.
TinyPortal © 2005-2018