Recent

Author Topic: Inheriting TForm1 has no effect  (Read 2165 times)

TRon

  • Hero Member
  • *****
  • Posts: 3947
Re: Inheriting TForm1 has no effect
« Reply #15 on: May 08, 2024, 11:55:58 am »
Code: Pascal  [Select][+][-]
  1. Application.CreateForm(TForm1, Form1);

*FACEPALM*
You are too fast my friend  :)

@yazigegeda:
Form2 is never created so there will not be any code executed. Hence the facepalm.
I do not have to remember anything anymore thanks to total-recall.

yazigegeda

  • New Member
  • *
  • Posts: 29
Re: Inheriting TForm1 has no effect
« Reply #16 on: May 08, 2024, 12:16:12 pm »
Code: Pascal  [Select][+][-]
  1. Application.CreateForm(TForm1, Form1);

*FACEPALM*
You are too fast my friend  :)

@yazigegeda:
Form2 is never created so there will not be any code executed. Hence the facepalm.



Unit1

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     procedure FormCreate(Sender: TObject); virtual;
  16.   private
  17.  
  18.   public
  19.  
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.FormCreate(Sender: TObject);
  32. begin
  33.    ShowMessage('Unit1 FormCreate');
  34. end;
  35.  
  36. end.
  37.  
  38.  
  39.  



Unit2


Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode ObjFPC}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  9.  
  10.   Unit1;
  11.  
  12.  
  13. type
  14.  
  15.   { TForm2 }
  16.  
  17.   TForm2 = class(TForm1)
  18.     procedure FormCreate(Sender: TObject); override;
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form2: TForm2;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm2 }
  33.  
  34. procedure TForm2.FormCreate(Sender: TObject);
  35. begin
  36.     inherited;
  37.     ShowMessage('Unit2 FormCreate');
  38. end;
  39.  
  40. end.
  41.  
  42.  


lpr

Code: Pascal  [Select][+][-]
  1. program classdemo;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   {$IFDEF HASAMIGA}
  10.   athreads,
  11.   {$ENDIF}
  12.   Interfaces, // this includes the LCL widgetset
  13.   Forms, Unit1, Unit2
  14.   { you can add units after this };
  15.  
  16. {$R *.res}
  17.  
  18. begin
  19.   RequireDerivedFormResource:=True;
  20.   Application.Scaled:=True;
  21.   Application.Initialize;
  22.   Application.CreateForm(TForm1, Form1);
  23.   Application.CreateForm(TForm2, Form2);
  24.   Application.Run;
  25. end.
  26.  


But it does FormCreate twice :-\



Zvoni

  • Hero Member
  • *****
  • Posts: 2836
Re: Inheriting TForm1 has no effect
« Reply #17 on: May 08, 2024, 12:24:04 pm »
Remove Line 22 in lpr
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 3947
Re: Inheriting TForm1 has no effect
« Reply #18 on: May 08, 2024, 12:26:45 pm »
But it does FormCreate twice :-\
It does exactly what you told it to.

1. Create form 1, it executes create
2. Create form 2, it executes the create of form 2, first inheriting form1's create then the rest of the code

If you have specific needs/requirements then you would have to specifically state these. 

from one of your earlier posts:
Quote
My request is to inherit FormCreate from Unit1 in the newly created Unit2, so that it first calls FormCreate from the newly created Unit2, but now it does not execute the Unit2 code.  Or is there a way to create a Unit1 window in Unit2?
Thus switch the inherited create and show the message to accomplish that what you wrote above. Though it is still unclear for me if you want 2 separate forms or only one.

I do not have to remember anything anymore thanks to total-recall.

Zvoni

  • Hero Member
  • *****
  • Posts: 2836
Re: Inheriting TForm1 has no effect
« Reply #19 on: May 08, 2024, 12:37:47 pm »
Thus switch the inherited create and show the message to accomplish that what you wrote above. Though it is still unclear for me if you want 2 separate forms or only one.
I think he only wants one, and everything so far looks like "testing the waters" / "proof of concept" to me.

Think of designing only one Form, but using it in different units and different ways.
Prime example for this would be the "Dialogue"-Form - SaveAs, Open, etc.... you design the "look" of the Form once, but use it from different points and in different ways during runtime
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

yazigegeda

  • New Member
  • *
  • Posts: 29
Re: Inheriting TForm1 has no effect
« Reply #20 on: May 08, 2024, 12:41:13 pm »
But it does FormCreate twice :-\
It does exactly what you told it to.

1. Create form 1, it executes create
2. Create form 2, it executes the create of form 2, first inheriting form1's create then the rest of the code

If you have specific needs/requirements then you would have to specifically state these. 

from one of your earlier posts:
Quote
My request is to inherit FormCreate from Unit1 in the newly created Unit2, so that it first calls FormCreate from the newly created Unit2, but now it does not execute the Unit2 code.  Or is there a way to create a Unit1 window in Unit2?
Thus switch the inherited create and show the message to accomplish that what you wrote above. Though it is still unclear for me if you want 2 separate forms or only one.


The original idea was that I needed to create a Unit2(without a form) that inherited the TForm1 FormCreate and FormDestroy processes from Unit1, but I didn't know how to do that

alpine

  • Hero Member
  • *****
  • Posts: 1345
Re: Inheriting TForm1 has no effect
« Reply #21 on: May 08, 2024, 12:58:54 pm »
The original idea was that I needed to create a Unit2(without a form) that inherited the TForm1 FormCreate and FormDestroy processes from Unit1, but I didn't know how to do that
Still not clear.

Inheritance is a part of the object model. Each time you wrote:
Code: Pascal  [Select][+][-]
  1. type TMyForm=class(TForm)
You've got in TMyForm everything (except strict private) from TForm.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

 

TinyPortal © 2005-2018