Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Align objects at runtime
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
IRC channel
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Forum slow
by
Curt Carpenter
[
Today
at 03:41:07 am]
Arabic text, problem on L...
by
Zaher
[
Today
at 03:29:16 am]
InstallAware Using Lazaru...
by
TRon
[
Today
at 02:55:41 am]
Generics - correct syntax
by
Blaazen
[
Today
at 01:57:40 am]
Demoscene The Champs Crac...
by
Gigatron
[
Today
at 01:05:16 am]
How to use the Event Log?
by
n7800
[
Today
at 12:47:25 am]
v3.99 code completion que...
by
440bx
[
Today
at 12:45:55 am]
FpDebug breakpoint on "be...
by
440bx
[
Today
at 12:36:23 am]
Access violation when re-...
by
TRon
[April 19, 2024, 11:44:51 pm]
[solved] how to get class...
by
jamie
[April 19, 2024, 11:34:44 pm]
Lazarus for Windows on aa...
by
Wallaby
[April 19, 2024, 10:52:25 pm]
Poll: Watches and Display...
by
440bx
[April 19, 2024, 07:13:51 pm]
Who is Indy mattias?
by
paweld
[April 19, 2024, 04:17:53 pm]
I just released a commerc...
by
BrassGear
[April 19, 2024, 03:17:28 pm]
Does anyone know why thes...
by
Laksen
[April 19, 2024, 03:04:14 pm]
Database standards OR Am ...
by
gidesa
[April 19, 2024, 02:37:56 pm]
How to: create DLL file f...
by
TRon
[April 19, 2024, 02:26:53 pm]
A fairly simple sound sol...
by
paweld
[April 19, 2024, 01:46:11 pm]
Access violation when ope...
by
Чебурашка
[April 19, 2024, 12:27:34 pm]
Step-into the field sette...
by
Martin_fr
[April 19, 2024, 11:31:48 am]
AI, NLP and CAI: Text Gen...
by
Dzandaa
[April 19, 2024, 11:03:26 am]
dwindows for Android
by
PierceNg
[April 19, 2024, 10:54:44 am]
create system unit from s...
by
Laksen
[April 19, 2024, 10:53:57 am]
[Solved] Find child contr...
by
Joanna
[April 19, 2024, 09:53:43 am]
FpDebug unexpected Assemb...
by
Marc
[April 19, 2024, 08:46:38 am]
« previous
next »
Print
Pages: [
1
]
2
Author
Topic: [SOLVED] Align objects at runtime (Read 10590 times)
tudi_x
Hero Member
Posts: 532
[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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
Button1
.
AnchorSide
[
akBottom
]
.
Side
:
=
asrCenter
;
Button1
.
AnchorSide
[
akBottom
]
.
Control
:
=
Button2
;
end
;
thank you
«
Last Edit: September 04, 2017, 04:07:47 pm by tudi_x
»
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
taazz
Hero Member
Posts: 5368
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]
[+]
[-]
button1
.
top
:
=
(
(
button2
.
top
+
button2
.
height
)
div
2
)
-
(
button1
.
height
div
2
)
;
?
Logged
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
Hero Member
Posts: 532
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?
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
taazz
Hero Member
Posts: 5368
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.
Logged
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
Hero Member
Posts: 532
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
Button1
.
Top
:
=
AlignVertica
(
Button1
,
Button2
)
;
end
;
function
TForm1
.
AlignVertica
(
AObjToAlign
:
TWinControl
;
AllignTo
:
TWinControl
)
:
integer
;
begin
Result
:
=
round
(
AllignTo
.
Top
+
AllignTo
.
Height
div
2
-
AObjToAlign
.
Height
div
2
)
;
end
;
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
howardpc
Hero Member
Posts: 4144
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
with
Button1
do
begin
// centre button in form
AnchorSideLeft
.
Control
:
=
Self
;
AnchorSideLeft
.
Side
:
=
asrCenter
;
AnchorSideTop
.
Control
:
=
Self
;
AnchorSideTop
.
Side
:
=
asrCenter
;
BorderSpacing
.
Around
:
=
10
;
end
;
with
Button2
do
begin
// centre horizontally, and place below Button1
AnchorSideLeft
.
Control
:
=
Self
;
AnchorSideLeft
.
Side
:
=
asrCenter
;
AnchorSideTop
.
Control
:
=
Button1
;
AnchorSideTop
.
Side
:
=
asrBottom
;
end
;
end
;
«
Last Edit: August 18, 2017, 11:10:28 am by howardpc
»
Logged
tudi_x
Hero Member
Posts: 532
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!
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
tudi_x
Hero Member
Posts: 532
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
var
l
:
TLabel
;
begin
l
:
=
TLabel
.
Create
(
self
)
;
l
.
Parent
:
=
self
;
l
.
Caption
:
=
'xxxx'
;
l
.
Visible
:
=
True
;
l
.
Top
:
=
50
;
l
.
Left
:
=
50
;
with
l
do
begin
//AnchorSideTop.Control := self; //works
//AnchorSideLeft.Control := self; //works
//AnchorSideBottom.Control:= self; //does not work
AnchorSideRight
.
Control
:
=
self
;
//does not work
//AnchorSide[akRight].Control := self; //does not work
end
;
end
;
«
Last Edit: August 25, 2017, 05:26:16 pm by tudi_x
»
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
taazz
Hero Member
Posts: 5368
Re: Align objects at runtime
«
Reply #8 on:
August 25, 2017, 05:33:49 pm »
remove the with statement it might work.
Logged
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
Hero Member
Posts: 532
Re: Align objects at runtime
«
Reply #9 on:
August 25, 2017, 05:37:17 pm »
removed and got the same results.
please advise
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
taazz
Hero Member
Posts: 5368
Re: Align objects at runtime
«
Reply #10 on:
August 25, 2017, 05:47:54 pm »
Quote from: tudi_x on August 25, 2017, 05:37:17 pm
removed and got the same results.
please advise
define does not work.
Logged
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
Hero Member
Posts: 532
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]
[+]
[-]
AnchorSideRight
.
Control
:
=
self
;
how come it works for the left side?
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
taazz
Hero Member
Posts: 5368
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].
Logged
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
Hero Member
Posts: 532
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
var
l
:
TLabel
;
begin
l
:
=
TLabel
.
Create
(
self
)
;
l
.
Parent
:
=
self
;
l
.
Caption
:
=
'xxxx'
;
l
.
Visible
:
=
True
;
l
.
Top
:
=
50
;
l
.
Color
:
=
clYellow
;
//l.Alignment := taRightJustify;
l
.
AutoSize
:
=
False
;
//l.Left := self.Width - l.Width;
l
.
Left
:
=
20
;
//l.AnchorSideRight.Control := self;
l
.
AnchorSide
[
akRight
]
.
Control
:
=
self
;
//no effect
Form1
.
Caption
:
=
IntToStr
(
self
.
Width
)
+
'|'
+
IntToStr
(
l
.
Width
)
+
'|'
+
IntToStr
(
l
.
Left
)
;
end
;
how come it works for the left side?
Logged
Lazarus 2.0.2 64b on Debian LXDE 10
balazsszekely
Guest
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]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
l
:
=
TLabel
.
Create
(
self
)
;
l
.
Parent
:
=
self
;
l
.
Caption
:
=
'xxxx'
;
l
.
Visible
:
=
True
;
l
.
Top
:
=
50
;
l
.
Color
:
=
clYellow
;
//l.Alignment := taRightJustify;
l
.
AutoSize
:
=
False
;
//l.Left := self.Width - l.Width;
l
.
Left
:
=
20
;
l
.
Top
:
=
(
Form1
.
Height
-
l
.
Height
)
div
2
;
end
;
procedure
TForm1
.
FormResize
(
Sender
:
TObject
)
;
begin
l
.
Top
:
=
(
Form1
.
Height
-
l
.
Height
)
div
2
;
end
;
Logged
Print
Pages: [
1
]
2
« previous
next »
Lazarus
»
Forum
»
Free Pascal
»
Beginners
(Moderators:
FPK
,
Tomas Hajny
) »
[SOLVED] Align objects at runtime
TinyPortal
© 2005-2018