Recent

Author Topic: Automatically positioning controls when form is resized  (Read 1397 times)

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Automatically positioning controls when form is resized
« on: January 16, 2023, 02:22:08 pm »
Dear ALL,

I am stuck with a ridiculous problem: I have a regular form containing a TMemo component and three buttons. I want that these controls (both the memo and the buttons) to adjust automatically to the form when the 'maximize' button of the form is clicked, but cannot devise a way of doing this! I presume the buttons should be 'anchored' to the main form, but could not make it work. If I put the TMemo inside a TScrollBar (adjusting the 'AutoSize' property), it works but the position of the buttons is not affected.

Could someone give me some hints on how solving this?

A sample project is attached.

Thanks a lot for any assistance you can provide!
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

Ally

  • Jr. Member
  • **
  • Posts: 81
Re: Automatically positioning controls when form is resized
« Reply #1 on: January 16, 2023, 03:26:00 pm »
Hello Maurobio,

attached is your example, extended by the corresponding information in the anchor editor.

Greetings Roland

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Automatically positioning controls when form is resized
« Reply #2 on: January 16, 2023, 03:33:26 pm »
Dear @Ally,

Thanks a lot, it worked fine! When setting the anchors, I was using the opposite combinations you have used for the TMemo and the buttons 'Anchors' property, so it not worked!

Again, thanks for taking your time to help me solve such a simple question!

Best wishes,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

paweld

  • Hero Member
  • *****
  • Posts: 1568
Re: Automatically positioning controls when form is resized
« Reply #3 on: January 16, 2023, 03:34:41 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   Memo1.Anchors := [akTop, akLeft, akRight, akBottom];
  4.   Button1.AnchorSideTop.Control := Memo1;
  5.   Button1.AnchorSideTop.Side := asrBottom;
  6.   Button1.BorderSpacing.Top := 16;
  7.   Button2.AnchorSideLeft.Control := Button1;
  8.   Button2.AnchorSideLeft.Side := asrBottom;
  9.   Button2.BorderSpacing.Left := 16;
  10.   Button2.AnchorSideTop.Control := Button1;
  11.   Button2.AnchorSideTop.Side := asrTop;
  12.   Button2.BorderSpacing.Top := 0;
  13. end;
  14.  
  15. procedure TForm1.FormResize(Sender: TObject);
  16. begin
  17.   Button1.Left := (Form1.Width div 2) - Button1.Width - 8;
  18. end;
Best regards / Pozdrawiam
paweld

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Automatically positioning controls when form is resized
« Reply #4 on: January 16, 2023, 08:22:20 pm »
Dear @paweld,

Thanks for your tricky programmatic solution - it allows the buttons to appear centralized in the form, even when it is resized!  ;)

With best wishes,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

wp

  • Hero Member
  • *****
  • Posts: 13361
Re: Automatically positioning controls when form is resized
« Reply #5 on: January 16, 2023, 11:12:35 pm »
You can even center the buttons without writing any code: Since you want an odd number of buttons (3), use the Anchor Editor to center the middle one in the form (use the middle button in the "Left anchoring box). Then anchor the right side of the previous button (the one immediately at the left) to the left side of the centered button (use the right button in the "left anchoring box"). And anchor the left side of the next button (the one immediately at the right) to the right side of the centered button (use the left button in the "right anchoring box" now).

If there are more than three buttons proceed with the remaining buttons in the analogous way.

To adjust the distance between the buttons set the BorderSpacing properties.

If there is an even number of buttons you can add a bevel to the form, set its Shape to bsSpacer so that it become invisible and set its Width to the desired distance between the buttons. Then anchor the left button to the bevel in the same way as you did with the left button, and repeat the idea with the right button.

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Automatically positioning controls when form is resized
« Reply #6 on: January 17, 2023, 01:18:52 am »
Dear @wp,

Thanks, yours were very good suggestions because in some forms I may in fact have three or more buttons.

With best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

 

TinyPortal © 2005-2018