Recent

Author Topic: Bug: TPageControl.Anchors  (Read 1289 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Bug: TPageControl.Anchors
« on: March 22, 2021, 07:39:29 am »
I prefer setting GUI components on runtime. Maintaining and inspecting becomes easier because I don't have to click the component and looking for the property in the Object Inspector.

But I've just found TPageControl.Anchors behaves weird. It works correctly if I set the Anchors on design time but not if I do it on runtime. Did I do it wrong?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, ComCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     PageControl1: TPageControl;
  18.     TabSheet1: TTabSheet;
  19.     TabSheet2: TTabSheet;
  20.     TabSheet3: TTabSheet;
  21.     procedure FormCreate(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.   PageControl1.Anchors := [akTop, akBottom, akLeft, akRight];
  36.   Edit1.Anchors        := [akTop, akLeft, akRight];
  37.   Button1.Anchors      := [akBottom, akLeft, akRight];
  38. end;
  39.  
  40. end.

I tested it on Lazarus 2.0.10 GTK2 Ubuntu Mate. Can you reproduce the issue? Should I submit a bug report?

Below is the screenshot, left one - design time and right one - runtime:

dseligo

  • Hero Member
  • *****
  • Posts: 1653
Re: Bug: TPageControl.Anchors
« Reply #1 on: March 22, 2021, 08:15:45 am »
It seems fine on Windows 10, Laz 2.0.10.

Michl

  • Full Member
  • ***
  • Posts: 226
Re: Bug: TPageControl.Anchors
« Reply #2 on: March 22, 2021, 09:27:39 am »
I haven't looked in you app, but I think there is no bug, but not enough defined. I think you want to achieve something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   PageControl1.Anchors := [akTop, akBottom, akLeft, akRight];  
  4.  
  5.   // you have to set a control - if nil, parent is used
  6.   Edit1.AnchorSideTop.Control := TabSheet1;
  7.   Edit1.AnchorSideTop.Side := asrTop;
  8.   Edit1.AnchorSideLeft.Control := TabSheet1;
  9.   Edit1.AnchorSideLeft.Side := asrTop;
  10.   // you can also use xyz.AnchorSide[akLeft], xyz.AnchorSide[akTop] etc.
  11.   Edit1.AnchorSide[akRight].Control := TabSheet1;
  12.   Edit1.AnchorSide[akRight].Side := asrBottom;
  13.   // maybe, you want some space
  14.   Edit1.BorderSpacing.Around := 10;    
  15.   Edit1.Anchors        := [akTop, akLeft, akRight];
  16.  
  17.   // for Button1 you have to do the same
  18.   Button1.Anchors      := [akBottom, akLeft, akRight];
  19. end;  

See https://wiki.lazarus.freepascal.org/Anchor_Sides
« Last Edit: March 22, 2021, 09:30:26 am by Michl »
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

wp

  • Hero Member
  • *****
  • Posts: 13353
Re: Bug: TPageControl.Anchors
« Reply #3 on: March 22, 2021, 09:58:31 am »
Without wanting to explicitly anchor all sides of the controls as in Michl solution, I think that the OnCreate event is too early. There are many properties which do not yet have their final values, and this may depend in detail on the widgetset.

But if I apply the anchoring commands later in the OnActivate event everything works fine.

Handoko

  • Hero Member
  • *****
  • Posts: 5515
  • My goal: build my own game engine using Lazarus
Re: Bug: TPageControl.Anchors
« Reply #4 on: March 23, 2021, 02:46:10 am »
Ok, I understood now. Thank you all.

 

TinyPortal © 2005-2018