Recent

Author Topic: calculate position of TButton's on a TForm  (Read 395 times)

paule32

  • Sr. Member
  • ****
  • Posts: 439
calculate position of TButton's on a TForm
« on: April 22, 2025, 06:18:24 pm »
Hello,
I have an Array of TButton's (10 TButton)
Now, I would place them horizontal on the Form with a for loop.
How can I do this, when I have only the Counter Variable btnCount ?
The Buttons shall have a gap of 10 Pixels on each.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1505
    • Lebeau Software
Re: calculate position of TButton's on a TForm
« Reply #1 on: April 22, 2025, 06:24:21 pm »
I have an Array of TButton's (10 TButton)
Now, I would place them horizontal on the Form with a for loop.
How can I do this, when I have only the Counter Variable btnCount ?
The Buttons shall have a gap of 10 Pixels on each.

Something like this:

Code: Pascal  [Select][+][-]
  1. var
  2.   iLeft, iTop, idx: Integer;
  3.   arr: array of TButton;
  4.   btn: TButton;
  5. begin
  6.   arr := ...;
  7.   iLeft := ...; // desired starting position
  8.   iTop := ...; // desired starting position
  9.   for idx := Low(arr) to High(arr) do
  10.   begin
  11.     btn := arr[idx];
  12.     btn.Left := iLeft;
  13.     btn.Top := iTop;
  14.     Inc(iLeft, btn.Width + 10);
  15.   end;
  16. end;

Or:

Code: Pascal  [Select][+][-]
  1. var
  2.   iLeft, iTop: Integer;
  3.   arr: array of TButton;
  4.   btn: TButton;
  5. begin
  6.   arr := ...;
  7.   iLeft := ...; // desired starting position
  8.   iTop := ...; // desired starting position
  9.   for btn in arr do
  10.   begin
  11.     btn.Left := iLeft;
  12.     btn.Top := iTop;
  13.     Inc(iLeft, btn.Width + 10);
  14.   end;
  15. end;
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

wp

  • Hero Member
  • *****
  • Posts: 12787
Re: calculate position of TButton's on a TForm
« Reply #2 on: April 22, 2025, 07:28:57 pm »
In Lazarus the container controls have a little-known property named ChildSizing (https://wiki.freepascal.org/Autosize_/_Layout#Layout). It helps to automatically position controls in this container.

I guess you do not want to fill the form completely by the 10 buttons. Therefore, I'd propose to add a TPanel to the form which will serve as a container for the buttons. Go to property ChildSizing of the panel. Set its "ControlsPerLine" to 10 (or 5, if you want to have the buttons in two rows), and set "Layout" to "cclLeftToRightThenTopToBottom". Now drop the 10 buttons on the panel - each button will be automatically positioned and prefectly aligned. In order to control the spacing between the buttons enter their distance in "HorizontalSpacing" (and "VerticalSpacing" if you have multiple rows). If you want to rearrange the button order, select the button to be moved and press Ctrl+PgUp or Ctrl+PgDown. Finally set the panel's AutoSize to true, and you may probably also want to bottom-align the panel (Align = alBottom, or similar).

paule32

  • Sr. Member
  • ****
  • Posts: 439
Re: calculate position of TButton's on a TForm
« Reply #3 on: April 22, 2025, 09:33:16 pm »
Thanks Remy
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

lainz

  • Hero Member
  • *****
  • Posts: 4705
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: calculate position of TButton's on a TForm
« Reply #4 on: April 23, 2025, 04:53:07 am »
Thanks wp is the solution I wanted to write

 

TinyPortal © 2005-2018