Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if the object is created or not in delphi

Tags:

delphi

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

like image 545
naren Avatar asked Sep 14 '11 13:09

naren


3 Answers

Sometimes form has been free but it is not nil. In such case the check of Assigned is not so good. So one option is to free the form and set MyForm:=nil on OnClose form. another option is to use the following proc:

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;
like image 126
Yos863 Avatar answered Sep 20 '22 09:09

Yos863


if Assigned(Form1) then
begin
  //form is created
end;

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

like image 42
Linas Avatar answered Sep 22 '22 09:09

Linas


Assigned(Obj) can still return True even after you free it, using "Obj.free". The best way to assure your obj is free, FOR USING Assigned(obj) is using "FreeAndNil(Obj)"

like image 30
user1897277 Avatar answered Sep 21 '22 09:09

user1897277