Sign in Join
Wiki copytemporary

Anchor Sides: what changed

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

Title: Anchor Sides

OldNewLine
1+There are some new [[Property|properties]] and [[Method|methods]] for automatic layout of controls. You can now setup controls to keep a certain distance to other controls, or center relative to other controls. See below for examples.
2+
3+Each of the four sides of a control (Left, Top, Right, Bottom) can now be anchored/bound to a side of another control. For example you can now anchor the left side of [[TEdit]] to the right side of a [[TLabel]]. Everytime the Label is moved or resized the Edit's left side will follow, which normally results in moving the Edit parallel to the Label.
4+
5+## Example 1
6+
7+`+--------+ +-------+`\
8+`| Label1 | | Edit1 |`\
9+`+--------+ |       |`\
10+`           +-------+`
11+
12+### In code
13+
14+``` Pascal
15+Edit1.AnchorSide[akLeft].Side := asrRight;
16+Edit1.AnchorSide[akLeft].Control := Label1;
17+Edit1.Anchors := Edit1.Anchors + [akLeft];
18+```
19+
20+You can define the distance with the BorderSpacing properties:
21+
22+``` Pascal
23+Edit1.BorderSpacing.Left := 10;
24+```
25+
26+The same can be done with the method:
27+
28+``` Pascal
29+Edit1.AnchorToNeighbour(akLeft, 10, Label1);
30+```
31+
32+### Notes
33+
34+The Edit1.Left will follow Label1.Left+Label1.Width, not the other way around. That means, moving Label1 will move Edit1. But moving Edit1 will be undone by the [[LCL]]. If you also anchor the right side of Label1 to the left side of Edit1, you created a circle, and this can result together with some other autosize properties in a loop. This is ignored by the LCL or automagically repaired when the Parent.AutoSize is [[True|true]].
35+
36+### Via the Anchor Editor
37+
38+The anchor editor is a floating window that is available via the menu **View / Anchor Editor** or via the button on the **Anchors** property in the object inspector.
39+
40+<figure>
41+<img src="Anchor_Editor_en.png" title="Anchor_Editor_en.png" />
42+<figcaption>Anchor_Editor_en.png</figcaption>
43+</figure>
44+
45+## Example 2
46+
47+You can anchor the Edit's top side to follow the Label's top side:
48+
49+ +--------+ +-------+
50+ | Label1 | | Edit1 |
51+ +--------+ | |
52+ +-------+
53+
54+``` Pascal
55+Edit1.AnchorSide[akTop].Side := asrTop;
56+Edit1.AnchorSide[akTop].Control := Label1;
57+Edit1.Anchors := Edit1.Anchors + [akTop];
58+```
59+
60+The same can be done with the method:
61+
62+``` Pascal
63+Edit1.AnchorParallel(akTop,0,Label1);
64+```
65+
66+## Example 3
67+
68+Centering a Label vertically to an Edit:
69+
70+ +-------+
71+ +--------+ | |
72+ | Label1 | | Edit1 |
73+ +--------+ | |
74+ +-------+
75+
76+``` Pascal
77+Edit1.AnchorSide[akTop].Side := asrCenter;
78+Edit1.AnchorSide[akTop].Control := Label1;
79+Edit1.Anchors := Edit1.Anchors + [akTop] - [akBottom];
80+```
81+
82+The same can be done with the method:
83+
84+``` Pascal
85+Edit1.AnchorVerticalCenterTo(Label1);
86+```
87+
88+Obviously anchoring the bottom side of Edit1 does not make sense when centering.
89+
90+## Example 4
91+
92+Using a [[TSplitter]] with the AnchorEditor between two panels: the left panel should behave as if it had `Align=alLeft`, the right panel should behave as if it had `Align=alClient`.
93+
94+- Do not use the `Align` properties of the panels and of the splitter. They must be set to `alNone`. This is important for the splitter for which the default value is `alLeft`.
95+- In the anchor editor do the following adjustments:
96+ - Anchor the top and bottom sides of the panels and the splitter to the corresponding sides of the form.
97+ - Select the first panel and anchor its left side to the left side of the form and its right side to the left side of the splitter.
98+ - Select the second panel and anchor its right side to the right side of the form and its left side to the right side of the splitter.
99+ - The left and right sides of the splitter must not be anchored (as seen from the splitter itself) because this would fix the position of the splitter and make it not movable.
100+
101+The same effect can be achieved by this code:
102+
103+``` pascal
104+procedure TForm1.FormCreate(Sender: TObject);
105+begin
106+ with Panel1 do
107+ begin
108+ AnchorSideTop.Control := Self;
109+ AnchorSideBottom.Control := Self;
110+ AnchorSideBottom.Side := asrBottom;
111+ AnchorSideLeft.Control := Self;
112+ AnchorSideRight.Control := Splitter1;
113+ Anchors := [akLeft, akRight, akTop, akBottom];
114+ end;
115+ with Panel2 do
116+ begin
117+ AnchorSideTop.Control := Self;
118+ AnchorSideBottom.Control := Self;
119+ AnchorSideBottom.Side := asrBottom;
120+ AnchorSideLeft.Control := Splitter1;
121+ AnchorSideLeft.Side := asrBottom;
122+ AnchorSideRight.Control := self;
123+ AnchorSideRight.Side := asrBottom;
124+ Anchors := [akLeft, akRight, akTop, akBottom];
125+ end;
126+ with Splitter1 do
127+ begin
128+ AnchorSideTop.Control := Self;
129+ Splitter1.AnchorSideBottom.Control := Self;
130+ AnchorSideBottom.Side := asrBottom;
131+ Anchors := [akTop, akBottom];
132+ end;
133+end;
134+```
135+
136+## New property
137+
138+``` Pascal
139+property AnchorSide[Kind: TAnchorKind]: TAnchorSide read GetAnchorSide;
140+```
141+
142+This is not published in the [[IDE Window: Object Inspector|object inspector]]. You can edit it in the designer via the [[IDE Window: Anchor Editor|anchor editor]] (Menu: View -\> View anchor editor, or click on button of 'Anchors' property).
143+
144+## New methods to easily configure common layouts
145+
146+``` Pascal
147+procedure AnchorToNeighbour(Side: TAnchorKind; Space: integer; Sibling: TControl);
148+procedure AnchorParallel(Side: TAnchorKind; Space: integer; Sibling: TControl);
149+procedure AnchorHorizontalCenterTo(Sibling: TControl);
150+procedure AnchorVerticalCenterTo(Sibling: TControl);
151+procedure AnchorAsAlign(TheAlign: TAlign; Space: Integer);
152+```
153+
154+AnchorVerticalCenterTo works with Parent too. Then it will center on the client area, that means the center of the control is at *ClientHeight div 2*.
155+
156+Center anchoring is not yet fully supported when computing the size of the parent. For example when you put a label center anchored into a [[TGroupBox|Groupbox1]] and set Groupbox1.AutoSize to true then Groupbox1 height will shrink leaving no space for the label. The solution is to center to a control that is not centered. For example center a Label1 to a [[TComboBox|ComboBox]] and use for the ComboBox the default anchors (Anchors=\[akLeft,akTop\]).
157+
158+## Anchoring to invisible controls
159+
160+Anchoring to invisible controls works intuitively. For example: The below controls A, B, C are anchored (C.Left to B.Right and B.Left to A.Right):
161+
162+` +---+ +---+ +---+`\
163+` | A | | B | | C |`\
164+` +---+ +---+ +---+`
165+
166+If B is hidden (Visible:=[[False|false]]) then C.Left will skip B and use the right of A, resulting in
167+
168+` +---+ +---+`\
169+` | A | | C |`\
170+` +---+ +---+`
171+
172+## Circular references
173+
174+You can build circular references by anchoring two sides to each other, which creates an impossible anchoring. The LCL notices that, but will not raise an exception, because it can be a temporary circle. For example: A row of buttons. Button1 is left of Button2, Button2 is left of Button3. Now the order is changed to: Button3, Button1, Button2. If the reanchoring is started with Button3, a circle is created temporarily and is fixed when the reordering is completed. The LCL detects circles and will not move the Button. Circles are not the only anomaly.
175+
176+There is one exception to this rule. If the Parent.AutoSize is true, then the LCL will automatically break circles on autosize and writes a warning via [[DebugLn|debugln]] (windows: --debug-log.txt, linux, et al: stdout). Which anchor is broken depends on the order of controls and the sides. So, temporary circles are still allowed with AutoSize=true if you enclose the change in
177+
178+``` Pascal
179+Parent.DisableAutosizing;
180+try
181+ // change anchors, aligns, bounds...
182+finally
183+ Parent.EnableAutosizing;
184+end;
185+```
186+
187+The AutoSize algorithm does not only break circles, but fixes Align/AnchorSide inconsistencies too.
188+
189+Eventually it would be good to add some hints in the designer or a tool to list the circles and inconsistencies.
190+
191+## See also
192+
193+- [[Autosize / Layout]]
194+- [[Example: Anchors. How to reliably align dynamically created controls under changing visibility]]
195+
196+---
197+*From the Free Pascal wiki, page "Anchor Sides", as it stood on 2021-10-11 after a change by Kai Burghardt, 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