Recent

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?
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

cdbc

  • Hero Member
  • *****
  • Posts: 2818
    • http://www.cdbc.dk
Re: Center a button
« Reply #1 on: February 25, 2026, 04:03:18 pm »
Hi
Hmmm, I'd go with:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   btnCenter.Left:= (Self.ClientWidth - btnCenter.Width) div 2;
  4.   btnCenter.Top:= (Self.ClientHeight - btnCenter.Height) div 2;
  5. end;
...dunno if there's a 'built-in' %)
Regards Benny
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

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 »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: Center a button
« Reply #4 on: February 25, 2026, 05:56:40 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.
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 »

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][+][-]
  1.  
  2.   MyButton.AnchorVerticalCenterTo(Form1);
  3.   MyButton.AnchorHorizontalCenterTo(Form1);
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

n7800

  • Hero Member
  • *****
  • Posts: 710
  • Lazarus IDE contributor
    • GitLab profile
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

 

TinyPortal © 2005-2018