Recent

Author Topic: [solved] How to find component in other unit with FindComponent  (Read 1210 times)

Muso

  • Sr. Member
  • ****
  • Posts: 356
I have basically the same question as in https://forum.lazarus.freepascal.org/index.php?topic=40192.0
but I still fail to get it right.

I have two units:
Code: Pascal  [Select][+][-]
  1. unit UnitA;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.   TMainForm = class(TForm)
  12.     RunTime1FSE: TFloatSpinEdit;
  13.     RunTime2FSE: TFloatSpinEdit;
  14.     RunTime3FSE: TFloatSpinEdit;
  15.     procedure DutyCycleXFSEChange(Sender: TObject);
  16.   private
  17.   public
  18.   end;
  19.  
  20. var
  21.   MainForm : TMainForm;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. uses
  28.   UnitB;
  29.  
  30. procedure TMainForm.DutyCycleXFSEChange(Sender: TObject);
  31. begin
  32.   PumpControl.PCDutyCycleXFSEChange(Sender);
  33. end;
  34.  
  35. end.

and

Code: Pascal  [Select][+][-]
  1. unit UnitB;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   UnitA, Classes, SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.   TPumpControl = class
  12.   private
  13.   public
  14.     procedure PCDutyCycleXFSEChange(Sender: TObject);
  15.   end;
  16.  
  17. var
  18.   PumpControl: TPumpControl;
  19.  
  20. implementation
  21.  
  22. {$R *.lfm}
  23.  
  24. procedure TPumpControl.PCDutyCycleXFSEChange(Sender: TObject);
  25. var
  26.  Step : integer;
  27. begin
  28.  Step:= 1;
  29.  (MainForm.FindComponent('RunTime' + IntToStr(Step) + 'FSE')
  30.         as TFloatSpinEdit).Increment:= 1;
  31. end;
  32.  
  33. end.


Everything compiles, but on runtime it crashes because FindComponent cannot find the component.
Has anybody an idea what my mistake is?
« Last Edit: April 12, 2021, 10:54:41 pm by Muso »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to find component in derived class with FindComponent
« Reply #1 on: April 12, 2021, 09:55:14 pm »
It is not clear what you are trying to do, and you show code, but not a compilable project where it would be clear how your objects are instantiated (if they are), and which methods, if any, are hooked to your spinedits.
However, you declare TPumpControl so that it inherits from TMainForm, and you redeclare the DutyCycleXFSEChange method, which is very strange, and wrong.

When you call DutyCycleXFSEChange you spiral into recursive calls.
Basically, your UnitB is a nonsense. What are you trying to achieve?



Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to find component in derived class with FindComponent
« Reply #2 on: April 12, 2021, 10:06:01 pm »
It is not clear what you are trying to do

I have a form with more than 100 objects. To keep the overview I want to have the main unitA that creates the form, and a unit B in which I have e.g. all the procedures for SpinEdits.
So From unitB I want to access the objects from the form. I'll provide a compilable example.

Quote
However, you declare TPumpControl so that it inherits from TMainForm, and you redeclare the DutyCycleXFSEChange method, which is very strange, and wrong.

What is wrong with this? I think I can reuse the same procedure name as described in https://wiki.freepascal.org/Class#Notes_on_inheritance
I gave now the procedure in UnitB another name, but the problem persists.

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to find component in derived class with FindComponent
« Reply #3 on: April 12, 2021, 10:40:48 pm »
So From unitB I want to access the objects from the form. I'll provide a compilable example.

Attached is now an example project as ZIP
« Last Edit: April 12, 2021, 10:53:12 pm by Muso »

Muso

  • Sr. Member
  • ****
  • Posts: 356
Re: How to find component in other unit with FindComponent
« Reply #4 on: April 12, 2021, 10:54:30 pm »
OK, I got it, my mistake was just that I used a derived class.  With an independent class, everything works.
Stupid me!

The example in my previous post shows how I implemented it.

 

TinyPortal © 2005-2018