Recent

Author Topic: How to list all data modules opened in an Application?  (Read 3637 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
How to list all data modules opened in an Application?
« on: March 06, 2013, 12:19:02 am »

When I am opening a form and need to check whether an instance of the form exists I use this code:

Code: [Select]
function FindFormT(formClass: string):TForm;
var
  i, j:integer;
  upFormClass: string;
begin
  result := nil;
  upformClass := 'T' + UpperCase(formClass);
  for i := 0 to Screen.FormCount-1 do
  begin
    if UpperCase(Screen.Forms[i].ClassName) = upFormClass then
    begin
      result := Screen.Forms[i];
    end;
 end;

end; 

What equivalent code should be used for data modules? Does the Application object have a data module count?
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to list all data modules opened in an Application?
« Reply #1 on: March 06, 2013, 06:07:01 am »

When I am opening a form and need to check whether an instance of the form exists I use this code:

Code: [Select]
function FindFormT(formClass: string):TForm;
var
  i, j:integer;
  upFormClass: string;
begin
  result := nil;
  upformClass := 'T' + UpperCase(formClass);
  for i := 0 to Screen.FormCount-1 do
  begin
    if UpperCase(Screen.Forms[i].ClassName) = upFormClass then
    begin
      result := Screen.Forms[i];
    end;
 end;

end; 

What equivalent code should be used for data modules? Does the Application object have a data module count?

No but the screen object does take a look at Screen.DataModuleCount; and  Screen.DataModules[] properties.
Just a question do you have forms in dlls?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How to list all data modules opened in an Application?
« Reply #2 on: March 06, 2013, 11:21:33 am »
No, I don't have forms in dlls. Is there some reason why you are asking?
No but the screen object does take a look at Screen.DataModuleCount; and  Screen.DataModules[] properties.
Just a question do you have forms in dlls?
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to list all data modules opened in an Application?
« Reply #3 on: March 06, 2013, 11:27:47 am »
because I would code the above routine as

Code: [Select]
function FindForm(const aClass:TClass):TForm;
var
  vCnt: Integer;
begin
  for vCnt := 0 to Screen.FormCount -1 do
    if Screen.Forms[vCnt].ClassType = aClass then begin
      Result := Screen.Forms[vCnt];
      Break;
    end;
end;

Calling it like

FindForm(TForm3);

that will not work if the form is in a dll though.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How to list all data modules opened in an Application?
« Reply #4 on: March 06, 2013, 01:22:50 pm »
You learn something new everyday :)
Do you use form dlls in Lazarus on Windows, and Linux as well? I am thinking of it as something that will minimize my compile-edit-debug cycles?

because I would code the above routine as

Code: [Select]
function FindForm(const aClass:TClass):TForm;
var
  vCnt: Integer;
begin
  for vCnt := 0 to Screen.FormCount -1 do
    if Screen.Forms[vCnt].ClassType = aClass then begin
      Result := Screen.Forms[vCnt];
      Break;
    end;
end;

Calling it like

FindForm(TForm3);

that will not work if the form is in a dll though.
Lazarus 3.0/FPC 3.2.2

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to list all data modules opened in an Application?
« Reply #5 on: March 06, 2013, 06:13:22 pm »
No I don't, using the forms from dlls will increase your debug cycles, you see any small change on the internals of your program might trigger a SIGSEGV exception on the dll if they are not compiled as well with the changes in. This makes it harder to debug not easier.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018