Вопрос

How to prevent a child from being creating multiple times, ex: a login form. Is there a solution without looping over the childs before the login form is getting opened and check if another instance of it is created.

Это было полезно?

Решение

By default, each Form has a global pointer declared. Even if you don't auto-create the Forms, you can still utilize that pointer. For any give Form, initialize its global pointer to nil at startup, check the pointer for nil before creating a new instance of that Form, destroy the Form when it is closed, and reset its global pointer back to nil when the Form is destroyed. For example:

unit LoginForm;

interface

...

type
  TLoginForm = class(TForm)
    ...
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    ...
  end;

var
  LoginForm: TLoginForm = nil; // <-- here

implementation

...

procedure TLoginForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TLoginForm.FormDestroy(Sender: TObject);
begin
  LoginForm := nil;
end;

...

end.

procedure TMainForm.SomeProcedure;
begin
  ...
  if not Assigned(LoginForm) then
    LoginForm := TLoginForm.Create(Self);
  LoginForm.Show;
  ...
end;
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top