Link to home
Start Free TrialLog in
Avatar of LeTay
LeTay

asked on

Detect existence of an object

Is there an easy and elegant way to detect if a specific object has been created ?
In my case, objects to verify are forms.
Thanks
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

If  you wan to search under parent window (ex. specific panel):
obj: TComponent;
...
obj := pnl1.FindChildControl('Comp1Name');
if Assigned(obj) then ....

Open in new window


if you need to serch on whole form:
obj := FindGlobalComponent('Form1.Comp1Name');
if Assigned(obj) then ....

Open in new window


or

obj := Form1.FindComponent('Comp1Name');
if Assigned(obj) then ....

Open in new window

Avatar of LeTay
LeTay

ASKER

Well in fact, the component I am trying to find if it still exists is that Form1, not a component inside it
So ?
So you can check if it's assigned by
If Assigned(Form1)

Open in new window

or using it's name
var 
  Form: TForm;
begin
  Form := Application.FindComponent('Form1') as TForm;
  if Assigned(Form) then
    Form.Show
  else
    { can't find it, so maybe you want to create it } 

Open in new window

Avatar of LeTay

ASKER

My idea was something like this, but it is not very elegant (I know that my forms have their Tag >= 0)

function FormExists(F:TForm):boolean;
begin
  try
    Result := (F.Tag >= 0);
  except
    Result := False;
  end;
end;
Form can be found using by Name or using Forms Caption + Class name:

if FindWindow('TForm1', 'FormsCaption') <> 0 then .... // form exists

Open in new window


or using Application as form owner:
...Application.FindComponent('Form1')...

Open in new window

Avatar of LeTay

ASKER

Is it faster than "my" method ?
I have to check existence of around a hundred forms ...
Avatar of LeTay

ASKER

Also, does the findcomponent return "false" if the form has not been created ?
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeTay

ASKER

Many thanks, this works fine