Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
LCL
»
Center a button
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
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
You can embed Windows Con...
by
Fibonacci
[
Today
at 04:18:40 am]
Conscious Artificial Inte...
by
schuler
[
Today
at 02:42:53 am]
Implementing an Elo ratin...
by
mas steindorff
[
Today
at 02:22:34 am]
[New Component] ExtTabCtr...
by
jianwt
[
Today
at 02:21:39 am]
Can /my/ AI help me with ...
by
microxa
[
Today
at 12:56:29 am]
IndySecOpenSSL is now ava...
by
TheMouseAUS
[
Today
at 12:08:51 am]
RunFormula: math expressi...
by
stormray
[June 17, 2026, 10:32:05 pm]
Codepage issue in console...
by
ASerge
[June 17, 2026, 09:36:27 pm]
Pdf Viewer in Pascal
by
Tomxe
[June 17, 2026, 07:18:20 pm]
Canvas size
by
Thaddy
[June 17, 2026, 07:15:50 pm]
[Reopened] TSaveDialog
by
Thaddy
[June 17, 2026, 05:12:17 pm]
What am I missing here? [...
by
Thaddy
[June 17, 2026, 03:39:02 pm]
Questions about 16 color ...
by
wp
[June 17, 2026, 01:45:07 pm]
TCHATGPT — An Artificial ...
by
Weiss
[June 17, 2026, 07:00:13 am]
FPC Unleashed (inline var...
by
440bx
[June 16, 2026, 11:53:51 pm]
RFC: Separation of MCU an...
by
ackarwow
[June 16, 2026, 11:06:14 pm]
Error with last fixes_3.2...
by
patyit
[June 16, 2026, 09:49:02 pm]
Mundo Medieval 3D MMORPG ...
by
Rodrigo Robles
[June 16, 2026, 06:06:17 pm]
Which Control should I us...
by
wp
[June 16, 2026, 05:08:55 pm]
Just Question App paramet...
by
eldonfsr
[June 16, 2026, 04:50:19 pm]
Content is distorting / w...
by
Handoko
[June 16, 2026, 03:53:02 pm]
ZxTune chiptunes player
by
Guva
[June 16, 2026, 12:41:14 pm]
Onscroll event for Tscrol...
by
laz_one_or2
[June 16, 2026, 11:16:39 am]
Fpcupdeluxe
by
CharlyTango
[June 16, 2026, 10:35:33 am]
Compiling Qt6 project on ...
by
wp
[June 16, 2026, 10:14:53 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Center a button (Read 964 times)
LemonParty
Hero Member
Posts: 537
Center a button
«
on:
February 25, 2026, 03:55:17 pm »
I want a button to be centered exactly to center of the form. Should I calculate position or there is another way to do this?
Logged
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11
cdbc
Hero Member
Posts: 2818
Re: Center a button
«
Reply #1 on:
February 25, 2026, 04:03:18 pm »
Hi
Hmmm, I'd go with:
Code: Pascal
[Select]
[+]
[-]
procedure
TForm1
.
FormCreate
(
Sender
:
TObject
)
;
begin
btnCenter
.
Left
:
=
(
Self
.
ClientWidth
-
btnCenter
.
Width
)
div
2
;
btnCenter
.
Top
:
=
(
Self
.
ClientHeight
-
btnCenter
.
Height
)
div
2
;
end
;
...dunno if there's a 'built-in'
Regards Benny
Logged
If it ain't broke, don't fix it
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release & FPC Main -> Lazarus Main
Ally
Jr. Member
Posts: 83
Re: Center a button
«
Reply #2 on:
February 25, 2026, 04:50:41 pm »
Hello LemonParty,
Here is an example that uses the anchor editor.
Greetings Roland
Logged
Handoko
Hero Member
Posts: 5551
My goal: build my own game engine using Lazarus
Re: Center a button
«
Reply #3 on:
February 25, 2026, 05:43:56 pm »
Or you can try my code for resizing and centering an object on the center of a form using calculation:
https://forum.lazarus.freepascal.org/index.php/topic,43424.msg303879.html#msg303879
«
Last Edit: February 25, 2026, 05:45:33 pm by Handoko
»
Logged
JuhaManninen
Global Moderator
Hero Member
Posts: 4715
I like bugs.
Re: Center a button
«
Reply #4 on:
February 25, 2026, 05:56:40 pm »
Quote from: Ally on February 25, 2026, 04:50:41 pm
Here is an example that uses the anchor editor.
I was going to suggest anchor editor, too. I did not check the example project but setting Top and Left anchoring to the parent form should be enough.
Calculation in FormCreate works only if the form is never resized.
Logged
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.
Handoko
Hero Member
Posts: 5551
My goal: build my own game engine using Lazarus
Re: Center a button
«
Reply #5 on:
February 25, 2026, 06:55:42 pm »
Thank you for informing me the problem. I fixed it. I checked the date of the code I posted, it was 2018 ... I was so inexperience. :'(
«
Last Edit: February 25, 2026, 07:00:21 pm by Handoko
»
Logged
LemonParty
Hero Member
Posts: 537
Re: Center a button
«
Reply #6 on:
February 25, 2026, 08:16:59 pm »
Thank you.
I centered button with this code:
Code: Pascal
[Select]
[+]
[-]
MyButton
.
AnchorVerticalCenterTo
(
Form1
)
;
MyButton
.
AnchorHorizontalCenterTo
(
Form1
)
;
Logged
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11
n7800
Hero Member
Posts: 710
Lazarus IDE contributor
Re: Center a button
«
Reply #7 on:
February 25, 2026, 09:08:45 pm »
There's a great article about creating form layouts:
https://wiki.freepascal.org/Autosize_/_Layout
If you are more interested in the code, then this one would be more suitable:
https://wiki.freepascal.org/Anchor_Sides
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
LCL
»
Center a button
TinyPortal
© 2005-2018