Recent

Author Topic: Automatically size and position child controls  (Read 2482 times)

fedkad

  • Full Member
  • ***
  • Posts: 178
Automatically size and position child controls
« on: March 06, 2018, 04:25:12 pm »
This is a question about sizing and positioning child controls without coding.

Let's say, I have three buttons (or any other control) and I want them to be positioned and sized like this:

<10%>[20%]<10%>[20%]<10%>[20%]<10%>

<> : empty space
[] : button or any other control

This layout should be preserved when the parent control (say the form they reside in) is resized at run-time.

Is this possible to do without coding?
« Last Edit: March 06, 2018, 04:45:30 pm by fedkad »
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Automatically size and position child controls
« Reply #1 on: March 06, 2018, 04:56:56 pm »
Anchors, AnchorEditor, Alignment & Co. ...

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Automatically size and position child controls
« Reply #2 on: March 06, 2018, 04:57:07 pm »
try setting all anchors to false
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

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: Automatically size and position child controls
« Reply #3 on: March 06, 2018, 05:03:48 pm »
Thank you for your answers. But, I was asking whether positioning and sizing is possible.

The buttons are positioned as expected, but their size remain the same.
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: Automatically size and position child controls
« Reply #4 on: March 06, 2018, 05:16:01 pm »
Try something like this...  :)

Code: Pascal  [Select][+][-]
  1. Procedure TForm1.SetBtnPos(Btn1, Btn2, Btn3: TButton);
  2.   Var
  3.    iSpace, iBtnWidth: Integer;
  4.  Begin
  5.   iSpace   := (ClientWidth*10) Div 100;
  6.   iBtnWidth:= (ClientWidth-iSpace*4) Div 3;
  7.  
  8.   Btn1.SetBounds(iSpace                      , 0, iBtnWidth, 50);
  9.   Btn2.SetBounds(iSpace*2+iBtnWidth          , 0, iBtnWidth, 50);
  10.   Btn3.SetBounds(ClientWidth-iSpace-iBtnWidth, 0, iBtnWidth, 50);
  11.  End;
  12.  
  13.  
  14. Procedure TForm1.FormResize(Sender: TObject);
  15.  Begin
  16.   SetBtnPos(Button1, Button2, Button3);
  17.  End;

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: Automatically size and position child controls
« Reply #5 on: March 06, 2018, 05:20:02 pm »
Yes. I can do that with code. But, I was asking if this is possible without coding:)
« Last Edit: March 06, 2018, 05:31:54 pm by fedkad »
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Automatically size and position child controls
« Reply #6 on: March 06, 2018, 05:37:44 pm »
there is an open source container, if memory serves me right it was named Tsizepanel or something along those lines, that does what you want.
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

fedkad

  • Full Member
  • ***
  • Posts: 178
Re: Automatically size and position child controls
« Reply #7 on: March 06, 2018, 06:13:55 pm »
The above was an oversimplification of what I was going to achieve.

My goal is this: Assume you have form with a dozen of various types of controls positioned and sized very carefully on the form. I would like the form to be resizable, so that when the end user changes the size of the form, all controls will resize proportionately and keep their relative positions on the form.

I accept that you can do this in code: store somewhere the original positions and sizes of all the controls and when the form is resized change all the positions and sizes of the controls according to the form resize factor.

But, I was wondering if there is simpler solution.
Lazarus 4.0 / FPC 3.2.2 on x86_64-linux-gtk2 (Ubuntu/GNOME) and x86_64-win64-win32/win64 (Windows 11)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Automatically size and position child controls
« Reply #8 on: March 06, 2018, 06:22:13 pm »
the panel I mentioned does exactly that for you with out code. You simple drop it on the form set its alignment to alclient place your controls in it (not the form) and every time the form is resized it will auto size the controls in it according to you specifications. Sorry I have no link to give you you will have to search for it.
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

 

TinyPortal © 2005-2018