tags:

views:

339

answers:

3

What is your preferred way of keeping controls centered on its parent when the parent change width or height?

+4  A: 

Set control's Anchors property to [akLeft, akTop, akRight, akBottom].

gabr
This is indeed the way to do it.
Gamecat
This assumes the childcontrol is allowd to resize.
Vegar
Yes. The other answer describes the non-resizing approach.
gabr
+5  A: 

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
A: 

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