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
Parameter passing odditie...
by
KodeZwerg
[
Today
at 04:43:09 pm]
what to do if my target M...
by
nickysn
[
Today
at 04:16:02 pm]
trunk compiler bug for mi...
by
nickysn
[
Today
at 04:03:44 pm]
Offical launch of the 1 B...
by
BeniBela
[
Today
at 03:52:05 pm]
compiler error in unit
by
paule32
[
Today
at 03:49:50 pm]
Xcode Command Line Tools ...
by
Martin_fr
[
Today
at 03:45:26 pm]
Compile/Convert Delphi pr...
by
Joseph
[
Today
at 02:43:45 pm]
how to tweek fpmake to co...
by
Key-Real
[
Today
at 02:33:27 pm]
Can function be used for ...
by
VisualLab
[
Today
at 02:15:30 pm]
Multithreading - synchron...
by
mika
[
Today
at 01:48:51 pm]
A fairly simple sound sol...
by
KodeZwerg
[
Today
at 01:48:26 pm]
May be useful to somebody
by
KodeZwerg
[
Today
at 12:38:22 pm]
match automatic highlight...
by
Чебурашка
[
Today
at 11:35:14 am]
Tchart with dual numbers ...
by
wp
[
Today
at 10:48:18 am]
Generics - correct syntax
by
VisualLab
[
Today
at 10:23:16 am]
add new targets to make
by
Key-Real
[
Today
at 09:38:23 am]
Debugger crashes with App...
by
Martin_fr
[
Today
at 09:29:46 am]
Copyrights Q
by
BrassGear
[
Today
at 09:13:14 am]
[SOLVED] how to build the...
by
Key-Real
[
Today
at 09:12:33 am]
Quirky windows
by
tetrastes
[
Today
at 08:59:06 am]
How can I draw a rectangl...
by
Handoko
[
Today
at 07:30:55 am]
Draw a line in the Scroll...
by
Handoko
[
Today
at 07:27:54 am]
web development using pas...
by
Nate897
[
Today
at 02:04:05 am]
questionable function def...
by
440bx
[
Today
at 01:35:28 am]
Why isn't TTIRadioGroup's...
by
jamie
[
Today
at 12:13:45 am]
« previous
next »
Print
Pages: [
1
]
2
Author
Topic: [SOLVED] Align objects at runtime (Read 10599 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