Recent

Author Topic: Container for non-visual components ?  (Read 2049 times)

FrankBKK

  • New Member
  • *
  • Posts: 22
Container for non-visual components ?
« 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.

lucamar

  • Hero Member
  • *****
  • Posts: 4142
Re: Container for non-visual components ?
« Reply #1 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zvoni

  • Hero Member
  • *****
  • Posts: 627
Re: Container for non-visual components ?
« Reply #2 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
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

wp

  • Hero Member
  • *****
  • Posts: 8555
Re: Container for non-visual components ?
« Reply #3 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.
« Last Edit: June 24, 2021, 04:04:21 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

FrankBKK

  • New Member
  • *
  • Posts: 22
Re: Container for non-visual components ?
« Reply #4 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 !

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 955
    • Lebeau Software
Re: Container for non-visual components ?
« Reply #5 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.
« Last Edit: June 25, 2021, 03:46:43 am by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

egsuh

  • Hero Member
  • *****
  • Posts: 783
Re: Container for non-visual components ?
« Reply #6 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