Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why form.show is called before form.create in firemonkey?

Everyone know tell me why Form.show is called before that Form.create if property Form.visible = true.

I tested in Delphi XE7 and Delphi 10 Seattle (Fmx form compiled for windows)

Ex:


    procedure TForm1.FormCreate(Sender: TObject);
    var
      i : integer;
    begin
      //break point here is called before if form.visible = false
      i := 0;
    end;

procedure TForm1.FormShow(Sender: TObject);
var
  i : integer;
begin
  //break point here is called before if form.visible = true
  i := 0;
end;

like image 285
Maycoll Trevezani Avatar asked Sep 23 '16 18:09

Maycoll Trevezani


1 Answers

If the main form is not set to be visible in the designer then the call to CreateMainForm() will force the form to be visible after construction is complete (and therefore after OnCreate has fired).

procedure TApplication.CreateMainForm;
var
  I: Integer;
begin
  if FMainForm = nil then
  begin
     // here creating form...
  end;

  if FMainForm <> nil then
    FMainForm.Visible := True;  //** force visible here
end;

Otherwise, the form's Visible property is set during the call to TCommonCustomForm.Loaded(), which is called by the DFM streaming system during form construction, and thus triggers OnShow when the form becomes visible. OnCreate is not called until after construction is complete, after the DFM is streamed.

The bottom line is that you should not be making assumptions about when these events will be executed. If you need to control the order in which things happen, you'll need to do it explicitly.

like image 164
J... Avatar answered Nov 14 '22 03:11

J...