Sign in Join
Wiki copytemporary

Frames: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**[[TFrame|TFrames]]** are named containers for components and very similar to [[TForm|forms]]. Their unique ability is that they can be embedded into forms or other frames in the designer. As forms they are stored in two files: the code is stored in .pas file and the design in the .lfm file.
22
33## How to create a frame?
44
55Press File-\>New... menu item and in the opened dialog select "Frame" item.
66
7-<figure>
8-<img src="Frame_new.PNG" title="Frame_new.PNG" />
9-<figcaption>Frame_new.PNG</figcaption>
10-</figure>
7+![](/wiki-images/6/6d/Frame_new.PNG)
118
129## How to place a frame to the form or another frame?
1310
1411The [[Standard tab]] of the [[Component Palette]] has a special component item "[[TFrame]]". When you drop it to the form or frame the IDE asks you to choose one of project frames. You have to save the project and the the new frame first, only saved frames will appear in the choose from list. Frames in packages are not yet implemented. But you can create frames in code even in packages. You can't create frame circles - the IDE will forbid this, i.o.w. you can't place FrameA to FrameA and you can't place any frame which contains FrameA to FrameA.
1512
16-<figure>
17-<img src="Frame_add.PNG" title="Frame_add.PNG" />
18-<figcaption>Frame_add.PNG</figcaption>
19-</figure>
13+![](/wiki-images/3/35/Frame_add.PNG)
2014
2115## What can they be used for?
2216
2317They are needed when you have a group of components that you want to reuse on multiple forms. The group should have the same set of controls and logic between them at different windows (forms) of your application. You can group that repeated controls and logic into one frame and use that frame in different places. Therefore you don't need to repeat the work of control layouting and writing their logic.
2418
2519For example, you have two listboxes and buttons to move items between them. So you can create a frame with two listboxes and needed buttons, write the logic for items moving and then use your frame on all forms where needed. Moreover, if you find an error in the frame code then you can fix it once in the frame code instead of fixing it n-times among all forms.
2620
2721a frame in designer:
2822
29-<figure>
30-<img src="Frame_example.PNG" title="Frame_example.PNG" />
31-<figcaption>Frame_example.PNG</figcaption>
32-</figure>
23+![](/wiki-images/1/13/Frame_example.PNG)
3324
3425the frame placed on a form:
3526
36-<figure>
37-<img src="Frame_embedded.PNG‎" title="Frame_embedded.PNG‎" />
38-<figcaption>Frame_embedded.PNG‎</figcaption>
39-</figure>
27+![](/wiki-images/d/d4/Frame_embedded.PNG%E2%80%8E)
4028
4129the same frame placed on another form:
4230
43-<figure>
44-<img src="Frame_embedded2.PNG‎" title="Frame_embedded2.PNG‎" />
45-<figcaption>Frame_embedded2.PNG‎</figcaption>
46-</figure>
31+![](/wiki-images/3/36/Frame_embedded2.PNG%E2%80%8E)
4732
4833## Initialising private variables
4934
5035TFrame does not have OnCreate or OnDestroy events in which private variables can be initialised and released. [[Override]] the default [[Constructor|constructor]] and [[Destructor|destructor]] to do this.
5136
5237``` pascal
5338TFrame1 = class(TFrame)
5439 private
5540 MyObj: TObject;
5641 public
5742 constructor Create(TheOwner: TComponent); override;
5843 destructor Destroy; override;
5944 end;
6045
6146constructor TFrame1.Create(TheOwner: TComponent);
6247begin
6348 inherited Create(TheOwner);
6449 MyObj := TObject.Create;
6550end;
6651
6752destructor TFrame1.Destroy;
6853begin
6954 MyObj.Free
7055 inherited Destroy;
7156end;
7257```
7358
7459## Sample of dynamic creation
7560
7661More easy. Because TFrame component in component palette is not required. And you can make a page change type application(like a smart phone application) with save resource.
7762
7863``` pascal
7964unit Unit1;
8065
8166{$mode objfpc}{$H+}
8267
8368interface
8469
8570uses
8671 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
8772
8873type
8974
9075 { TForm1 }
9176
9277 TForm1 = class(TForm)
9378 Button1: TButton;
9479 GroupBox1: TGroupBox;
9580 procedure Button1Click(Sender: TObject);
9681 procedure FormCreate(Sender: TObject);
9782 private
9883 { private declarations }
9984 Frame: TFrame;
10085 public
10186 { public declarations }
10287 end;
10388
10489var
10590 Form1: TForm1;
10691
10792implementation
10893uses
10994 Unit2{TFrame1}, Unit3{TFrame2}, Unit4{TFrame3};
11095
11196{$R *.lfm}
11297
11398{ TForm1 }
11499
115100procedure TForm1.FormCreate(Sender: TObject);
116101begin
117102 Frame := TFrame1.Create(GroupBox1);
118103 Frame.Parent := GroupBox1;
119104end;
120105
121106procedure TForm1.Button1Click(Sender: TObject);
122107begin
123108 if not Assigned(Frame) then
124109 begin
125110 Frame := TFrame1.Create(GroupBox1);
126111 Frame.Parent := GroupBox1;
127112 end else if Frame is TFrame1 then begin
128113 Frame.Free;
129114 Frame := TFrame2.Create(GroupBox1);
130115 Frame.Parent := GroupBox1;
131116 end else if Frame is TFrame2 then begin
132117 Frame.Free;
133118 Frame := TFrame3.Create(GroupBox1);
134119 Frame.Parent := GroupBox1;
135120 end else begin
136121 FreeAndNil(Frame);
137122 end;
138123end;
139124
140125end.
141126```
142127
143128## See also
144129
145130- [[TFrame]]
146131
147132---
148133*From the Free Pascal wiki, page "Frames", as it stood on 2020-05-12 after a change by Jesusrmx, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history