What is your preferred way of keeping controls centered on its parent when the parent change width or height?
Asked
Active
Viewed 1,991 times
3 Answers
7
If by 'centered' you mean "it was already in the middle and you want to keep it there without resizing it", then remove all anchors. If it should be resized, gabr's solution is the one to with :)

Vincent Van Den Berghe
- 5,425
- 2
- 31
- 40
5
Set control's Anchors property to [akLeft, akTop, akRight, akBottom].

gabr
- 26,580
- 9
- 75
- 141
-
This is indeed the way to do it. – Toon Krijthe Nov 24 '08 at 09:50
-
This assumes the childcontrol is allowd to resize. – Vegar Nov 24 '08 at 10:10
-
Yes. The other answer describes the non-resizing approach. – gabr Nov 24 '08 at 10:27
0
If you mean a sort of "updating, please wait..." type thing, I manually move it in the Form's OnResize event. This allows me to keep a panel out of the way during design, and hidden normally, but I can make it visible when needed.
procedure TMyForm.FormResize(Sender: TObject);
var
nNewTop : Integer;
begin
inherited;
pnlRegenerating.Left := (ClientWidth - pnlRegenerating.Width) div 2;
nNewTop := (ClientHeight div 5) {* 4};
if (nNewTop + pnlRegenerating.Height) > ClientHeight then
nNewTop := ClientHeight - pnlRegenerating.Height - 4;
pnlRegenerating.Top := nNewTop;
end;

mj2008
- 6,647
- 2
- 38
- 56