What is the difference between these Form Delphi creation options?

Asked

Viewed 1,405 times

0

We have the following options:

Application.CreateForm(TForm, Form);

and

Form := TForm.Create(Application);

There’s a difference between the two?

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

What exactly is the difference if Application is proprietary?

  • As far as I know there is no difference, it is simply different construction methods. About não estará disponível para outros formulários can explain this part better?

  • Even after researching and putting the question here I insisted on the answer and really have difference, Application.Createform Creates a global variable so the form is seen in any part of the application, already Tform.Create creates a variable inside the form where it was instantiated and is therefore not available in other instances generating the false impression that it is available in another form, I will answer this question and try to detail how it works.

  • Reading your answer I understood your question, the part in the question: não estará disponível para outros formulários wasn’t so clear! Make an issue in the question and improve that part where you doubt it. + 1 by Reply.

2 answers

2

Placing the Difference Between Forms Construction Methods in Delphi

1 - Application.CreateForm(TForm, Form);

Creates 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, example roughly:

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 ( provided that added to Unit Formclient in USES )

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

Even saying that the owner is Application it will not be available in a call from a Showmodal form subsequent to it, example 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); Creates a local variable, so trying to use the form in other Units will give the Accessviolation error, because the form (variable) was not created globally

Another way of understanding:

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 you 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 vpublica variable will be available in any part of the current form and any form you wish to use this Unit

Descriptive summary:

I confused the creation of the Form with the creation of the Form Variable, I explain, when creating the form we can say who owns the form, if the application or others, this is necessary to know for the moment of destruction of the application, for the correct release of memory, already the creation of the variable whether it will be global or local, will 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 was clear.

  • The Owner of the form and its visibility are completely different things... I think it’s mixing. If I have on startup of the app Application.Createform(Tform3, Form3); Form5:=Tform5.Create(application); Both form3 and form5 are public and accessible from anywhere. I can call form5 in form3 at will...

  • The example here is for Rms created at runtime.

  • So Tiago, if I create the formularies there at the start of the application I agree with you, will be global variables, no matter how create, but my example shows the creation at runtime after the call of a screen for example.

2

The difference is that with Application.Createform the created form will be the "Mainform" (main form) if it is not yet defined. There are no other differences with respect to the visibility or use of the form.

  • So Ricardo, I don’t know if I’m not understanding the case of creation, but in practice using Showmodal, it doesn’t work like this, if you create a form before with Form := Tform.Create will not have the Form variable available inside the Modal Form created after, then at least in practice it makes a difference, as I explained I confused the owner of the form with the variable that receives the form object.

  • Marcelo, it is only a matter of declaring a variable with the appropriate scope. If there is a visible variable there will be no difference in Showmodal.

  • If you put the form Unit in the uses clause and the variable is declared in the interface session (of the same form), it will be visible globally.

  • I would like to have a functional example of this question, because I know that many, but many programmers pick on this issue, at first I see no difference between creating Application.Create and Form := Tform.Create, it turns out that there is a concrete difference, because even making the appropriate references in Units the form created with the second option may not be available in the open Form Modal, I do not know if the Stack allows link to download, I could make a simple example and put the provision to explain better what I am saying.

  • If you provide the link I will download so we can analyze better.

  • Follow the link to see what I mean... http://mvsoftware.com.br/downloads/TesteFormCreate.rar

  • Marcelo, in his example, Fdetalhes creates Fsubdetalhes in the constructor. Thus, when Fdetalhes is created with: "Fdetalhes := Tfdetalhes.Create(Application);", its constructor is being invoked before the address of the created object is assigned to the Fdetalhes variable. That’s why Fsubdetails cannot access Fdetalhes "yet". Normally, we try to encapsulate Forms so that they are not so dependent on each other. If Fsubdetails access to Fdetalhes is done after the assignment ends, the variable will be visible regardless of the method used to call the constructor.

  • Yes Ricardo, I noticed this, but note that in the first form it is created completely before any execution and does not generate error, so there is a difference between the creation methods, I am trying to understand the differences so that I use each creation method in its proper place to end memory errors in my applications, I was doing a test with Splash and I realized that I can’t use Application.Createform at the beginning, because when I close the Form Splash it closes the application, already with Tform.Create this does not happen.

  • Yes, when Mainform is closed the application shuts down. This is the main attribute of Mainform and the main effect of Application.CreateForm. Note that this method does a whole gymnastics to assign the address of the object to var Reference and then runs Create.

  • I understand that when Mainform closes it means that the application is gone, but there is the possibility of displaying a status Splash until Mainform and the like are fully loaded and then closing Splash and showing Mainform, for what I did here this routine happens in Applicaton.Run, it would be at the beginning of the beginning... so I need to create Splash before Mainform... if I create with Create(nil) it works, if create with Application.Createform does not work, it seems to me that at this point I have to say that nobody owns the Form Splash, I want those ideas clarified.

  • That’s correct. You could use Tsplashform.Create(Application), but there is no need.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.