| 
   
    | ...know whether a form already exist before you dynamically create it? |   
    | Autor: 
      Thomas Stutz |  | [ Print tip 
] |  |  |  
 
 
{Q: How to know whether a form already exist before I dynamically create it ?
 
 A: See the Forms and FormCount property of TScreen. You can iterate
 through the forms, and test to see if your form is there.
 }
 
 function IsFormOpen(const FormName : string): Boolean;
 var
 i: Integer;
 begin
 Result := False;
 for i := Screen.FormCount - 1 DownTo 0 do
 if (Screen.Forms[i].Name = FormName) then
 begin
 Result := True;
 Break;
 end;
 end;
 
 // Example: Showing a TForm.
 // First check, if the Form (here Form2) is open. If not, create it.
 
 procedure TForm1.Button1Click(Sender: TObject);
 begin
 if not IsFormOpen('Form2') then
 Form2 := TForm2.Create(Self);
 
 Form2.Show
 end;
 
 { For MDI Children }
 
 function IsMDIChildOpen(const AFormName: TForm; const AMDIChildName : string): Boolean;
 var
 i: Integer;
 begin
 Result := False;
 for i := Pred(AFormName.MDIChildCount) DownTo 0 do
 if (AFormName.MDIChildren[i].Name = AMDIChildName) then
 begin
 Result := True;
 Break;
 end;
 end;
 
 // Example: Showing a MDI Child.
 // First check, if the MDI Child is open. If not, create it.
 
 procedure TForm1.Button2Click(Sender: TObject);
 begin
 if not IsMDIChildOpen(Form1, 'MyMDIChild') then
 MyMDIChild := TMyMDIChild.Create(Self);
 
 MyMDIChild.Show;
 MyMDIChild.BringToFront;
 end;
 
 
 
   |