* * *

Author Topic: [SOLVED] Align objects at runtime  (Read 1309 times)

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
[SOLVED] Align objects at runtime
« on: August 17, 2017, 04:20:58 pm »
hi All,
i have some objects that i am creating at run time. i would like to know the easiest way to align their centers on vertical.
i there something built in for this?

the below did not move the buttons center at run time.

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Button1.AnchorSide[akBottom].Side := asrCenter;
  4.   Button1.AnchorSide[akBottom].Control := Button2;
  5. end;

thank you
« Last Edit: September 04, 2017, 04:07:47 pm by tudi_x »

taazz

  • Hero Member
  • *****
  • Posts: 4402
Re: Align objects at runtime
« Reply #1 on: August 17, 2017, 04:26:06 pm »
so what is wrong is wrong with
Code: Pascal  [Select]
  1. button1.top := ((button2.top+button2.height) div 2) - (button1.height div 2);
?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #2 on: August 17, 2017, 04:36:12 pm »
i made the post in the light of example 2 in http://lazarus-ccr.sourceforge.net/docs/lcl/controls/anchoring%20controls.html.
i thought i found a different way than directly computing the coordinates.

could you please advise if example 2 should actually move the buttons?

taazz

  • Hero Member
  • *****
  • Posts: 4402
Re: Align objects at runtime
« Reply #3 on: August 17, 2017, 04:41:36 pm »
I think anchoring gets involved only when button2 is moved not when button1 is sized/moved.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #4 on: August 17, 2017, 04:55:34 pm »
thank you.
i will stick with the below.

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.    Button1.Top:= AlignVertica(Button1, Button2);
  4. end;
  5.  
  6. function TForm1.AlignVertica(AObjToAlign: TWinControl; AllignTo: TWinControl): integer;
  7. begin
  8.   Result:= round(AllignTo.Top + AllignTo.Height div 2 - AObjToAlign.Height div 2);
  9. end;

howardpc

  • Hero Member
  • *****
  • Posts: 2388
Re: Align objects at runtime
« Reply #5 on: August 17, 2017, 06:14:19 pm »
For anchoring to work correctly you have to set two (or more) anchors. You can centre buttons on a form with code like this:
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   with Button1 do begin  // centre button in form
  4.     AnchorSideLeft.Control:=Self;
  5.     AnchorSideLeft.Side:=asrCenter;
  6.     AnchorSideTop.Control:=Self;
  7.     AnchorSideTop.Side:=asrCenter;
  8.     BorderSpacing.Around:=10;
  9.   end;
  10.   with Button2 do begin // centre horizontally, and place below Button1
  11.     AnchorSideLeft.Control:=Self;
  12.     AnchorSideLeft.Side:=asrCenter;
  13.     AnchorSideTop.Control:=Button1;
  14.     AnchorSideTop.Side:=asrBottom;
  15.   end;
  16. end;
« Last Edit: August 18, 2017, 11:10:28 am by howardpc »

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #6 on: August 18, 2017, 09:38:40 am »
i would use the anchoring when i have at least two elements of kind center, top, bottom, right, left to relate to.
thank you!

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: [SOLVED] Align objects at runtime
« Reply #7 on: August 25, 2017, 05:24:01 pm »
how would an alignment with the right side of the form be obtained? (windows 7 Lazarus 1.8 RC4 64b)

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   l: TLabel;
  4.  
  5. begin
  6.   l := TLabel.Create(self);
  7.   l.Parent := self;
  8.   l.Caption := 'xxxx';
  9.   l.Visible := True;
  10.   l.Top := 50;
  11.   l.Left := 50;
  12.  
  13.   with l do
  14.   begin
  15.     //AnchorSideTop.Control := self;  //works
  16.     //AnchorSideLeft.Control := self; //works
  17.     //AnchorSideBottom.Control:= self;  //does not work
  18.     AnchorSideRight.Control := self;   //does not work
  19.     //AnchorSide[akRight].Control := self;  //does not work
  20.   end;
  21. end;
« Last Edit: August 25, 2017, 05:26:16 pm by tudi_x »

taazz

  • Hero Member
  • *****
  • Posts: 4402
Re: Align objects at runtime
« Reply #8 on: August 25, 2017, 05:33:49 pm »
remove the with statement it might work.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #9 on: August 25, 2017, 05:37:17 pm »
removed and got the same results.
please advise

taazz

  • Hero Member
  • *****
  • Posts: 4402
Re: Align objects at runtime
« Reply #10 on: August 25, 2017, 05:47:54 pm »
removed and got the same results.
please advise
define does not work.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #11 on: August 25, 2017, 05:51:48 pm »
does not work = the control does not move to the right side of the Form / self as a result of the anchoring.
as per my understanding the right side of the control (SideRight) should anchor with the control i indicate (.Control := self).

Code: Pascal  [Select]
  1.  AnchorSideRight.Control := self;  

how come it works for the left side?

taazz

  • Hero Member
  • *****
  • Posts: 4402
Re: Align objects at runtime
« Reply #12 on: August 25, 2017, 06:03:16 pm »
Why don't you use the anchors property of the control? In any case, before setting your anchorsides make sure that
1) the anchors property is empty ee set it to []
2) the autosize property is false

Those are only steps to make sure that nothing else overrides your settings.

But if I where you I would simple set the anchors property to [akRight].
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

tudi_x

  • Sr. Member
  • ****
  • Posts: 338
Re: Align objects at runtime
« Reply #13 on: August 25, 2017, 06:21:53 pm »
the anchor commands do not have any effect.
could you please help with a working example or is this a bug?

Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   l: TLabel;
  4.  
  5. begin
  6.   l := TLabel.Create(self);
  7.   l.Parent := self;
  8.   l.Caption := 'xxxx';
  9.   l.Visible := True;
  10.   l.Top := 50;
  11.   l.Color := clYellow;
  12.   //l.Alignment := taRightJustify;
  13.   l.AutoSize := False;
  14.  
  15.   //l.Left := self.Width - l.Width;
  16.   l.Left := 20;
  17.   //l.AnchorSideRight.Control := self;
  18.   l.AnchorSide[akRight].Control := self;  //no effect
  19.   Form1.Caption := IntToStr(self.Width) + '|' + IntToStr(l.Width) + '|' + IntToStr(l.Left);
  20. end;  

how come it works for the left side?

GetMem

  • Hero Member
  • *****
  • Posts: 2408
Re: Align objects at runtime
« Reply #14 on: August 25, 2017, 06:27:30 pm »
Just move l(TLabel) to the private section of the form, then:
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   l := TLabel.Create(self);
  4.   l.Parent := self;
  5.   l.Caption := 'xxxx';
  6.   l.Visible := True;
  7.   l.Top := 50;
  8.   l.Color := clYellow;
  9.   //l.Alignment := taRightJustify;
  10.   l.AutoSize := False;
  11.  
  12.   //l.Left := self.Width - l.Width;
  13.   l.Left := 20;
  14.   l.Top := (Form1.Height - l.Height) div 2;
  15. end;
  16.  
  17. procedure TForm1.FormResize(Sender: TObject);
  18. begin
  19.   l.Top := (Form1.Height - l.Height) div 2;
  20. end;
  21.  

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus