What's the difference between these options for creating Form Delphi?

0

We have the following options:

Application.CreateForm(TForm, Form);

and

Form := TForm.Create(Application);

Is there a difference between the two?

I know it exists because depending on where we call each of them the "Form" variable will not be available for other forms

What exactly is the difference if Application owns?

    
asked by anonymous 08.02.2018 / 13:18

2 answers

2

Placing the difference between Forms building methods in Delphi

1 - Application.CreateForm(TForm, Form);

Create a global scope variable that can be instantiated in any other Form, of course with the previous addition of the Unit in the Form where you want to use it, roughly example:

Application.CreateForm(TFormCliente, FormCliente);
  Application.CreateForm(TFormDetalhes, FormDetalhes);
    Application.CreateForm(TFormSubDetalhes, FormSubDetalhes);
    FormDetalhes.SubShowModal;

       ... rotina ...
       Aqui estará disponpivel o formulário FormCliente e FormDetalhes
       ... Aguarda FormSubDetalhes ser fechado para continuar

  FormDetalhes.ShowModal;

    Idem

FormCliente.ShowModal;

In this case the FormClient form would be available in the other two created later (since adding the unit FormCliente in USES)

2 - Form: = TForm.Create (Application);

Even saying that the owner is Application it will not be available in a call from a later ShowModal form to it, eg roughly:

FormCliente := TFormCliente.Create(Application);
  FormDetalhes := TFormDetalhes.Create(Application);
    FormSubDetalhes := TFormSubDetalhes.Create(Application);
    FormDetalhes.SubShowModal;

       ... rotina ...
       Aqui não estará disponpivel o formulário FormCliente nem FormDetalhes
       ... Aguarda FormSubDetalhes ser fechado para continuar

  FormDetalhes.ShowModal;

    Idem

FormCliente.ShowModal;

Form: = TForm.Create (Application); It creates a local variable, so when trying to use the form in other units it will give the AccessViolation error because the (variable) form was not created globally

Another way to understand:

Within a function we can create variables that will be of exclusive use of that function, example 1

function fTeste(): Integer;
var vlocal: Integer;
begin
  result := vlocal+1;
end;

If you try to use the vlocal variable outside the function it will have an error because the variable was not created globally.

Example 2:

public // Declarando a variável para ser publica (global)
  vpublica: Integer;

function fTeste(): Integer;
begin
  result := vpublica+1;
end;

In example 2 the variable vpublica will be available in any part of the current form and any form that wishes to use this unit

Descriptive summary:

I confused the creation of the Form with the creation of the Variable Form, I explain, when you are creating the form we can say who is the owner of the form, whether the application or others, this is necessary to know for the moment of destruction of the application , for the correct release of memory, whether the creation of the variable will be global or local, give you power to use it locally or globally, I thought that when we put "Application" the created form would be available in any and all part of the application .

I hope I have been clear.

    
09.02.2018 / 15:02
2

The difference is that with Application.CreateForm the created form will be the "MainForm" (main form) if it is not already defined. There are no other differences in visibility or use of the form.

    
10.02.2018 / 15:43