Author: Lou Adler
How can I make a function which will create a frame (given its class) in a new form?
Answer:
Maybe you should use an enumerated value which is an index of an array of your
frame classes. An example with a form using two frames in seperate units:
1 unit UnitTestForm;
2 3 interface4 5 uses6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, UnitFrameOne, UnitFrameTwo;
8 9 type10 {'class of' and enumeration}11 TFrameClass = classof TFrame;
12 TFrameEnm = (frm_one, frm_two);
13 14 {array of classes}15 TFrameClasses = array[TFrameEnm] of TFrameClass;
16 17 {test form}18 TForm1 = class(TForm)
19 btnCreateFrame1: TButton;
20 btnCreateFrame2: TButton;
21 procedure btnCreateFrame1Click(Sender: TObject);
22 procedure btnCreateFrame2Click(Sender: TObject);
23 private24 function CreateFrame(FrameEnm: TFrameEnm): TForm;
25 public26 end;
27 28 var29 Frames: TFrameClasses = (TFrameOne, TFrameTwo);
30 31 Form1: TForm1;
32 33 implementation34 35 {$R *.dfm}36 37 procedure TForm1.btnCreateFrame1Click(Sender: TObject);
38 var39 Form: TForm;
40 begin41 Form := Self.CreateFrame(frm_one);
42 Form.ShowModal;
43 end;
44 45 procedure TForm1.btnCreateFrame2Click(Sender: TObject);
46 var47 Form: TForm;
48 begin49 Form := Self.CreateFrame(frm_two);
50 Form.ShowModal;
51 end;
52 53 function TForm1.CreateFrame(FrameEnm: TFrameEnm): TForm;
54 var55 aFrame: TFrame;
56 begin57 Result := TForm.Create(nil);
58 aFrame := Frames[FrameEnm].Create(Result);
59 aFrame.Parent := Result;
60 aFrame.Align := alClient;
61 end;