0

I have two units in my project as follow:

1 - Connexion unit:

unit Connexion;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TFConn = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  FConn: TFConn;

implementation

{$R *.dfm}

uses MainForm;

procedure TFConn.Button1Click(Sender: TObject);
begin
  if not Assigned(FMain) then
    begin
      FMain := TFMain.CreateNew(Application);
      FMain.OnClose := FMain.FormClose;
      FMain.ShowModal;
    end;
end;

end.

2 - MainForm unit :

unit MainForm;

interface
   uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TFMain = class(TForm)
     Constructor  FormCreate(Sender: TObject);overload;
     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  end;
Var
  FMain : TFMain;
implementation

Constructor  TFMain.FormCreate(Sender: TObject);
var B :  TButton;
begin
   inherited;
   B := TButton.Create(Self);
   B.Parent := Self;
   B.Caption := 'Button2';
end;

procedure tfmain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FMain := Nil;
end;
end.

The problem is that the procedure FormCreate in MainForm unit not working , I know that I miss something in the declaration , because the procedure should fire while the creation of the FMain form .

The application run without any error , but it should create B button on the FMain form.

How can I do that?

Ilyes
  • 14,640
  • 4
  • 29
  • 55
  • This is a mix of something I can't make sense of (if `tfmain` is a main form, why do create it from a non main one?). Well, as first I would get rid of the global variables. – Victoria Aug 01 '17 at 10:48
  • @Victoria `MainForm` is not the main form of the application , Connexion is the main form of the application as you can see . – Ilyes Aug 01 '17 at 10:49
  • So why did you choose such a name? Won't that confuse? – David Heffernan Aug 01 '17 at 10:55

1 Answers1

5
Constructor  FormCreate(Sender: TObject);overload;

is wrong. It should be:

procedure FormCreate(Sender: TObject);

The other problem is that you must set the OnCreate event to refer to FormCreate. Do this in the events page of the object inspector.

That will also need you to have a dfm file for this form which you appear not to have. Once you restore the dfm file you can set the OnClose event handler in the same way. You'd need to switch to Create rather than CreateNew.

You can't set the OnCreate event handler in code because the form has already been created.

If there is a good reason not to have a dfm file and do everything in code then you could need to add a constructor. Do that by overriding the virtual constructor:

constructor Create(AOwner: TComponent); override;

Finally, very much of the code in the question looks dubious, shall I saw. Strange use of global variables. Odd naming. Setting important events like OnClose from outside the class. Testing against nil that suggests weak design. I think there are problems ahead.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Can I do that wothout a `DFM` file of the `MainForm` unit? – Ilyes Aug 01 '17 at 10:55
  • I think it's all explained in the answer, no? – David Heffernan Aug 01 '17 at 10:56
  • Thank you for the explanation – Ilyes Aug 01 '17 at 11:18
  • `TObject` or `TComponent` , because I try what you say and also fail – Ilyes Aug 05 '17 at 15:12
  • Well I did the same way you say , `constructor Create(AOwner: TObject); override;` but I think there is a wrong with the type `TObject` – Ilyes Aug 05 '17 at 15:20
  • OK, I see, that should be TComponent – David Heffernan Aug 05 '17 at 15:22
  • Yeah , That's it , Now the app run fine ,there is only one error about the dfm , because when I click the button to create the form I got an error msg about the resource (dfm) . You know where you can find me to discuss about this for 1 mn please. – Ilyes Aug 05 '17 at 15:25
  • You need to fix your attitude. You come back here, unaccept the answer, and expect me to debug a program I cannot see, providing me with nothing more than "I got an error message about the resource". Are you serious? That's the best you can do. No code and you can't even manage to transcribe the error message. Usually people that don't transcribe error messages are the same people that don't even read them and attempt to understand them. Are you that person? This is a question and answer site. I answered the question. If you don't agree fine. – David Heffernan Aug 05 '17 at 16:10
  • I get the same error in that question https://stackoverflow.com/q/41889732/6426692 , and your answer was _Use `CreateNew`_ , But I want to use `Create` instead without a dfm. hope that I describe well. – Ilyes Aug 05 '17 at 16:19
  • If you derive directly from `TForm` then I think you can get away with it, but why are you making life so hard. Add a .dfm. It doesn't need to have anything in it. You can still create controls dynamically. No point fighting the system. – David Heffernan Aug 05 '17 at 16:28