1

I have a delphi application that used a PageControl with a number of TabSheets. I also create new TabSheets at runtime and populate them with instances of predefined frames. These frames work well, except for the cosmetic problem of not centering on the TabSheet. I have tried to use Frame.Align := alClient, but that didn't do it. The relevant code follows:

CreateNewPage(3);
NewLimitedChoiceFrame := TLimitedChoiceFrame.Create(NewInputPage);
NewLimitedChoiceFrame.Parent := NewInputPage;

CreateNewPage creates a new instance of a TabSheet and makes the PageControl it's owner and parent. The result is assigned to the global variable NewInputPage.

Ken White
  • 123,280
  • 14
  • 225
  • 444
SysJames
  • 411
  • 3
  • 10
  • 1
    Don't forget to accept answers ;-) This comment will destroy itself in few minutes, please do not respond to it. Thanks! – TLama Jan 23 '13 at 22:13

1 Answers1

3

To centre a control in its parent do this:

procedure CentreControl(Control: TControl);
begin
  Control.Left := (Control.Parent.ClientWidth-Control.Width) div 2;
  Control.Top := (Control.Parent.ClientHeight-Control.Height) div 2;
end;

Call this function, passing the frame. Obviously you need to wait until you've assigned the parent before doing so.

If the page control can be re-sized at runtime, add a call to this function from the tabsheet's OnResize event. Or, as NGLN points out simply set the control's Anchors to [] and the VCL framework will take care off centring the control when its parent is resized.

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    +1 To keep the control centered when the page control can be resized at runtime, then `Control.Anchors = []` will do too. – NGLN Sep 27 '12 at 04:44
  • @NGLN I don't think so. To keep it centred, Left and Top need to be updated on each resize. – David Heffernan Sep 27 '12 at 05:05
  • 3
    @DavidHeffernan, NGLN is right (checked for XE2). If Align is set to alNone and no anchors are set, the control is centered inside its parent control. You can check the code in TWinControl.ArrangeControl. – Uwe Raabe Sep 27 '12 at 07:03