Check if an object is created or not in delphi

I am creating an application using Delphi 7. I have added one button in the main form. On this button, click I want to show a different form. I try to create a second form only if the user clicked this button for the first time. If the user clicks this button a second time, then the already created form should be displayed. Does the form object have any property through which we can directly check if it is already created or not?

+5
source share
4 answers
if Assigned(Form1) then
begin
  //form is created
end;

But if your form is declared locally globally , you need to make sure that it is initialized nil.

+10
source

, . , . :

function TMainForm.GetOtherForm: TMyForm;
begin
  if not Assigned(FOtherForm) then
    FOtherForm := TMyForm.Create(Self);
  Result := FOtherForm;
end;
+4

Assigned (Obj) can return True even after you release it using "Obj.free". The best way to ensure that your obj is free, USING Assigned (obj) uses "FreeAndNil (Obj)"

+2
source

Sometimes the form was free, but it is not zero. In this case, the Assigned check is not so good. So one option is to free the form and set MyForm: = nil on the OnClose form. another option is to use the following procedure:

function TMyForm.IsFormCreated: bool;
var i: Integer;
begin
  Result := False;
  for i := 0 to Screen.FormCount - 1 do
  begin
    if Screen.Forms[i] is TMyForm then
    begin
      Result := True;
      Break;
    end;
  end;
end;
0
source

All Articles