Recent

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

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Align objects at runtime
« Reply #15 on: August 25, 2017, 06:38:24 pm »
sorry. the label still does not move on the right side of the form.
to anchor top (AnchorSideTop.Control := self;) and left (AnchorSideLeft.Control := self;) it works.
but not to anchor on right.

what is the effect of AnchorSideRight? how is it used?
Lazarus 2.0.2 64b on Debian LXDE 10

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Align objects at runtime
« Reply #16 on: August 25, 2017, 06:39:20 pm »
how come it works for the left side?
as I said use the property anchors never said anything about command.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   l: TLabel;
  4. begin
  5.   l := TLabel.Create(self);
  6.   l.Parent := self;
  7.   l.Caption := 'this is a test';
  8.   l.Visible := True;
  9.   l.Top := 50;
  10.   l.Color := clYellow;
  11.   l.Left := self.Width - l.Width - 4;
  12.   l.Anchors:=[akRight, akTop];//sticks to the top right side.
  13.   //l.Anchors:=[akRight, akbottom];
  14.   Form1.Caption := IntToStr(self.Width) + '|' + IntToStr(l.Width) + '|' + IntToStr(l.Left);
  15. end;
  16.  
this should stick the label on its parent top, right side ee it will move elative to the top and right sides of the parent add an akbottom to make the label resize its height as the parent size changes. keep in mind that the property anchors is a set not a simple value.
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

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Align objects at runtime
« Reply #17 on: August 25, 2017, 08:22:33 pm »
To do this by anchoring alone (i. e. not manually calculating and setting a value for Left) you have to write this (setting two properties of AnchorSideRight):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   l: TLabel;
  4. begin
  5.   l := TLabel.Create(self);
  6.   l.Caption := 'xxxx';
  7.   l.Top := 50;
  8.   l.Color := clYellow;
  9.   l.AutoSize := False;
  10.   l.Anchors := [akRight];
  11.   l.AnchorSideRight.Control := Self;
  12.   l.AnchorSideRight.Side := asrRight;
  13.   l.Parent := Self;
  14.   Form1.Caption := IntToStr(self.Width) + '|' + IntToStr(l.Width) + '|' + IntToStr(l.Left);
  15. end;

You'll note that anchoring works by 'sticking' controls to other controls, not by setting Top, Left values.
Top is set in this code (the label's top side is not anchored).
Left is not set in this code, either by the programmer, or by the anchoring code. Hence the form Caption shows the value of Left unaltered from 0 (the value all integers are initialised to when the class is created), even though the control is placed well away from the Left of the form.
BTW, it is better to set the Parent property last. This avoids  various assignments cascading unnecessarily with every property change made after Parent is set.
« Last Edit: August 25, 2017, 08:27:23 pm by howardpc »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Align objects at runtime
« Reply #18 on: August 28, 2017, 10:59:02 am »
thank you!
i have managed now to align and arrange my TLabels in the TPanel:

Code: Pascal  [Select][+][-]
  1. P: TPanel;
  2.  
  3. procedure TForm1.FormCreate(Sender: TObject);
  4. var
  5.   l1: TLabel;  //top
  6.   l2: TLabel;  //middle
  7.   l3: TLabel;  //bottom
  8.  
  9. begin
  10.   l1 := TLabel.Create(P);
  11.   l1.Caption := 'top';
  12.   l1.Color := clYellow;
  13.   l1.AutoSize := False;
  14.   l1.Anchors := [akRight, akTop];
  15.   l1.AnchorSideRight.Control := P;
  16.   l1.AnchorSideRight.Side := asrRight;
  17.   l1.AnchorSideTop.Control := P;
  18.   l1.Alignment := taRightJustify;
  19.   l1.Parent := P;
  20.  
  21.   l2 := TLabel.Create(P);
  22.   l2.Caption := 'middle';
  23.   l2.Color := clWhite;
  24.   l2.AutoSize := False;
  25.   l2.Anchors := [akRight, akTop];
  26.   l2.AnchorSideRight.Control := P;
  27.   l2.AnchorSideRight.Side := asrRight;
  28.   l2.AnchorSideTop.Control := l1;
  29.   l2.AnchorSideTop.Side := asrBottom;
  30.   l2.BorderSpacing.Right:= 5;
  31.   l2.Alignment := taRightJustify;
  32.   l2.Parent := P;
  33.  
  34.   l3 := TLabel.Create(P);
  35.   l3.Caption := 'bottom';
  36.   l3.Color := clYellow;
  37.   l3.AutoSize := False;
  38.   l3.Anchors := [akRight, akTop];
  39.   l3.AnchorSideRight.Control := P;
  40.   l3.AnchorSideRight.Side := asrRight;
  41.   l3.AnchorSideTop.Control := l2;
  42.   l3.AnchorSideTop.Side := asrBottom;
  43.   l3.Alignment := taRightJustify;
  44.   l3.Parent := P;
  45.  
  46.   P.Height := l1.Height + l2.Height + l3.Height + 3;
  47. end;  
« Last Edit: August 28, 2017, 11:45:15 am by tudi_x »
Lazarus 2.0.2 64b on Debian LXDE 10

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: [SOLVED] Align objects at runtime
« Reply #19 on: September 04, 2017, 03:25:08 pm »
is it possible to anchor an object that is not created at runtime to an object created at runtime?

is it possible to anchor an object that is created at runtime in one class - let us say a modified TLabel to another object created at runtime in another class (classes in different units)?
how would i pass the anchor reference as the names are not valid at design time?
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Align objects at runtime
« Reply #20 on: September 04, 2017, 03:51:47 pm »
is it possible to anchor an object that is not created at runtime to an object created at runtime?

Of course. Drop a button and label on a new Lazarus project's main form. Complete the button OnClick event handler as follows:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   SysUtils, Forms, Controls, StdCtrls;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     CreateRuntimeLabelButton: TButton;
  14.     DesignTimeLabel: TLabel;
  15.     procedure CreateRuntimeLabelButtonClick(Sender: TObject);
  16.   private
  17.     FRuntimeLabel: TLabel;
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. implementation
  24.  
  25. {$R *.lfm}
  26.  
  27. procedure TForm1.CreateRuntimeLabelButtonClick(Sender: TObject);
  28. begin
  29.   FRuntimeLabel:=TLabel.Create(Self);
  30.   with FRuntimeLabel do begin
  31.     Caption:='Runtime label';
  32.     BorderSpacing.Around:=50;
  33.     AnchorSideLeft.Control:=Self;
  34.     AnchorSideTop.Control:=Self;
  35.     Parent:=Self;
  36.   end;
  37.   with DesignTimeLabel do begin
  38.     AnchorSideTop.Control:=FRuntimeLabel;
  39.     AnchorSideTop.Side:=asrCenter;
  40.     AnchorSideLeft.Control:=FRuntimeLabel;
  41.     AnchorSideLeft.Side:=asrRight;
  42.   end;
  43. end;
  44.  
  45. end.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Align objects at runtime
« Reply #21 on: September 04, 2017, 04:07:32 pm »
thank you Howard!

i made public an array of the objects that i create dynamically and now i can reference the first element of the array which i am sure would exist and have a horizontal coordinate.
Lazarus 2.0.2 64b on Debian LXDE 10

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: [SOLVED] Align objects at runtime
« Reply #22 on: September 04, 2017, 04:12:12 pm »
is it possible to anchor an object that is created at runtime in one class - let us say a modified TLabel to another object created at runtime in another class (classes in different units)?
how would i pass the anchor reference as the names are not valid at design time?
See the  little project attached.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: [SOLVED] Align objects at runtime
« Reply #23 on: September 04, 2017, 04:18:49 pm »
thank you
i love Lazarus IDE
Lazarus 2.0.2 64b on Debian LXDE 10

 

TinyPortal © 2005-2018