Doubts creation, manipulation and elimination of form

Asked

Viewed 1,081 times

1

I have the following doubts:

  • When creating a form, according to the code below, I need to destroy it somehow when it is finished by the user?

F_pesqcli := Tf_pesqcli.Create(self);

F_pesqcli.Showmodal;

  • How to ensure that a window is not opened twice?

  • How do I minimize a form internally, as in the image example below?

Form minimizado

1 answer

1


1. How to ensure the release of the memory form?

When creating a form, according to the code below, I need to destroy it somehow when it is finished by the user?

F_PesqCli := TF_PesqCli.Create(self);

The builder Create object pattern TComponent in Delphi receives by parameter (AOwner: TComponent)

If you pass the Owner as Self, you are saying that the current instance of where the code is owner of the component created, in the case of TForm. So when that Owner is destroyed, the form will also be.

If you want, when the user finishes a form, that it is destroyed, in the event onClose form just you set the desired action

Action := caFree;

Another measure is to manage the window life cycle Modal during their initialization:

Form := TForm.Create(nil);
try
  Form.ShowModal;
finally
  Form.Free;
end;

2. How to ensure that a window is not opened twice?

The safest way is to validate those already created in the application

function ValidaFormJaCriado(const ClassDoForm: TClass): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := 0 to Screen.FormCount - 1 do
  begin
    if Screen.Forms[I].Class = ClassDoForm then
    begin
      Result := True;
      Break;
    end;
  end;
end;

3. How do I minimize a form internally, as in the image example below?

The Main Form must be set to the property FormStyle for fsMDIForm
Children must have the parent form as Owner and FormStyle for fsMDIChild

  • 1

    Perfect, that’s exactly what I needed. Thank you very much

Browser other questions tagged

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