Recent

Author Topic: Using non-visual components in a separate DataModule  (Read 7537 times)

edvard

  • Full Member
  • ***
  • Posts: 172
Using non-visual components in a separate DataModule
« on: November 22, 2013, 06:39:29 am »
See this post for my original musing:
http://forum.lazarus.freepascal.org/index.php/topic,22683.msg134250.html#msg134250

From what I gather from the helpful folks on this forum, non-visual components can be loaded into a separate DataModule to keep them from cluttering up the form designer and accessed from the main form.  So I whipped up a test form with a few non-visual components loaded into a DataModule, put 'Unit2' (default name of the unit for the DataModule) in the 'Uses' section of Unit1, and now I can't for the life of me figure out how to access the functions from the main form.  Searching the forum and the web just turns up tutorials on how to use DataModules for database programming, and I'm not quite up to that yet.  Any example code somebody can point me to?
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

jmpessoa

  • Hero Member
  • *****
  • Posts: 2297
Re: Using non-visual components in a separate DataModule
« Reply #1 on: November 22, 2013, 07:02:51 am »
Any example code.... from unit1 try:

DataModule1.TFOO.tag:= 1

Greetings!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: Using non-visual components in a separate DataModule
« Reply #2 on: November 22, 2013, 12:31:03 pm »
Are you trying to use the main form objects from the datamodule unit? Then you'd need to add the main form unit to a uses section of the datamodule unit, then do similar to the above example but in reverse - Form1.something... within Unit2.

Just remember you cannot add Unit1 to the interface uses section of Unit2 because that would create a circular reference since you already have Unit2 in the uses section of Unit1. You must do that in the implementation section.
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

edvard

  • Full Member
  • ***
  • Posts: 172
Re: Using non-visual components in a separate DataModule
« Reply #3 on: November 23, 2013, 05:48:24 pm »
Any example code.... from unit1 try:

DataModule1.TFOO.tag:= 1

I'm pretty sure I tried that, but it didn't work....

Are you trying to use the main form objects from the datamodule unit?

No, the reverse; I'm trying to use components I've placed in the datamodule unit from the main form (Unit1).
Quote
Then you'd need to add the main form unit to a uses section of the datamodule unit, then do similar to the above example but in reverse - Form1.something... within Unit2.

That's what I tried to do, but in reverse.  For example, I make a Form that has a Button.  I make a new DataModule and put a TrayIcon in it.  I put 'Unit2' in the 'Uses' section of Unit1, and put this code in Unit1 to make the TrayIcon show when I click the Button.
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  TrayIcon1.Show;
end;

I get this error:
Code: [Select]
unit1.pas(34,12) Error: Identifier not found "TrayIcon1"
**A FEW BRAINCYCLES LATER... **

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  DataModule1.TrayIcon1.Show;
end;

OK, now I get it.  TurboRascal put it right in my face, but I didn't see it. %)
Quote
Just remember you cannot add Unit1 to the interface uses section of Unit2 because that would create a circular reference since you already have Unit2 in the uses section of Unit1. You must do that in the implementation section.

Yes, common sense prevents me from trying that. ;)

So, now I'm wondering now about what would be proper programming practice in this case.  Should I use the DataModule as my main Unit and put all the code for the Form in there?  Or is using a DataModule as a secondary container (per my example) better?.  I realize this is only necessary as projects grow big enough to make modularizing a better way to handle the code, but I'm learning what I can while I'm still a beginner.  8-)
« Last Edit: October 13, 2015, 07:23:57 am by edvard »
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

edvard

  • Full Member
  • ***
  • Posts: 172
Re: Using non-visual components in a separate DataModule
« Reply #4 on: November 24, 2013, 07:09:56 am »
Quote
Should I use the DataModule as my main Unit and put all the code for the Form in there?

Never mind, that doesn't work too well.  Or at least, not for the stage I'm at in programming...
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Using non-visual components in a separate DataModule
« Reply #5 on: November 24, 2013, 11:53:31 am »
Datamodule could a container of non visual components, but its really create for splitting your data and GUI things. The components on the designer are only for yourself on getting the properties.
If you really want to 'clean' your form, you also can create the non visual component @runtime

Code: [Select]
interface

type TForm1 = class(Tform)
private
  FTrayIcon : TTrayIcon;
public
  procedure FormCreate(sender : TObject)
end;

implementation 

procedure Tform1.Fromcreate(snder : TObject);
begin
   FTrayIcon := TTrayIcon.create(self);
end;
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

edvard

  • Full Member
  • ***
  • Posts: 172
Re: Using non-visual components in a separate DataModule
« Reply #6 on: November 25, 2013, 03:41:12 am »
Datamodule could a container of non visual components, but its really create for splitting your data and GUI things.
...

I kinda figured that's the case, but I was advised by others that it is possible and easy, and the Lazarus SQLdb Tutorial page says this:
Quote
In this example, DataModule1 had nothing more than a Connection and Transaction, but in a 'real' application, this container would typically also hold global non-visual components to be used by the application.

Quote
If you really want to 'clean' your form, you also can create the non visual component @runtime

Very interesting idea.  The only drawback is that the properties would not be easily done in the Object Inspector, but would have to be explicitly addressed in the code.  I could live with that if I needed to, though.  Thanks for the suggestion.
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

 

TinyPortal © 2005-2018