Author: Tomas Rutkauskas
What is the best way to avoid a form being created more than once in a MDI
application?
Answer:
1 unit WindowFunctions;
2 3 interface4 5 uses6 Classes, Forms;
7 8 function IsChildWindow(AFormClass: TFormClass; AiTag: integer): Boolean;
9 procedure CreateChildWin(AOwner: TComponent; AFormClass: TFormClass; AiTag:
10 integer);
11 12 implementation13 14 uses15 Dialogs, Controls;
16 17 function IsChildWindow(AFormClass: TFormClass; AiTag: integer): boolean;
18 var19 i: integer;
20 begin21 Result := False; {The window does not exist}22 for i := 0 to (Screen.FormCount - 1) do23 begin24 if (Screen.Forms[i] is AFormClass) and (AiTag = Screen.Forms[i].Tag) then25 begin26 {The window was found}27 Screen.Forms[i].BringToFront;
28 Result := True;
29 break;
30 end;
31 end;
32 end;
33 34 procedure CreateChildWin(AOwner: TComponent; AFormClass: TFormClass; AiTag:
35 integer);
36 begin37 ifnot IsChildWindow(AFormClass, AiTag) then38 begin39 with AFormClass.Create(AOwner) do40 begin41 Tag := AiTag;
42 end;
43 end;
44 end;
45 46 end.