Recent

Author Topic: [SOLVED] Resize multiple Controls proportionally to Form, when Form is maximized  (Read 435 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Hi Folks,

i searched the Forum but found nothing applying to my Issue (or more probably i used the wrong search-terms :D)

As follows:

I have a Form, which has 3 GroupBoxes (further called GB1, GB2, GB3) side by side.
BorderStyle of the Form is bsSizable.

Anchors are set, MinWidth/MinHeight are set for each GB as well as the Form itself (Sum of all GB-MinWidths plus Distances between GB's plus Anchor-Distance to Form)
GB1 is anchored to the Form itself (Top and Left)
GB2 is anchored to GB1 (Top and Right-Edge of GB1)
GB3 is anchored to GB2 (Top and Right-Edge of GB2) and the Form (Right)

If the User now clicks to maximize the Form, only GB3 is resized to fit the Form (As expected).

How do i get all three GB's to resize proportionally to each other?
e.g. Initial Values --> Widths when Form is reduced to minimum Size (covered by Size-Constraints)
GB1-Width=250
GB2-Width=400
GB3-Width=600
I'm not talking about the Height of the Controls. That's covered through the Anchors.
I'm talking about the Width, which i'd like to resize proportionally to the Form's Width and to each other

Do i have to calculate the ratios and set the new widths in the FormResize-Event?
I'd expect a poor performance that way.

Or is there a property of the "Anchoring" i can use?

Hope i was clear enough
« Last Edit: September 30, 2025, 04:18:19 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

dangersgromit

  • New member
  • *
  • Posts: 9
Re: Resize multiple Controls proportionally to Form, when Form is maximized
« Reply #1 on: September 30, 2025, 12:25:08 pm »
Hi Zvoni.

I think this can't be done by anchoring on its own. You need to calculate the new width when resizing.

Doing somethin like this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     GroupBox1: TGroupBox;
  16.     GroupBox2: TGroupBox;
  17.     GroupBox3: TGroupBox;
  18.     procedure FormResize(Sender: TObject);
  19.     procedure FormShow(Sender: TObject);
  20.     procedure GroupBox1Exit(Sender: TObject);
  21.     procedure GroupBox2Exit(Sender: TObject);
  22.     procedure GroupBox3Exit(Sender: TObject);
  23.   private
  24.     w1 : Double;
  25.     w2 : Double;
  26.     w3 : Double;
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.FormResize(Sender: TObject);
  41. begin
  42.   GroupBox1.Width := Trunc(Form1.Width * w1);
  43.   GroupBox2.Width := Trunc(Form1.Width * w2);
  44.   GroupBox3.Width := Trunc(Form1.Width * w3);
  45. end;
  46.  
  47. procedure TForm1.FormShow(Sender: TObject);
  48. begin
  49.   GroupBox1Exit(Sender);
  50.   GroupBox2Exit(Sender);
  51.   GroupBox3Exit(Sender);
  52. end;
  53.  
  54. procedure TForm1.GroupBox1Exit(Sender: TObject);
  55. begin
  56.   w1 := GroupBox1.Width / Form1.Width;
  57. end;
  58.  
  59. procedure TForm1.GroupBox2Exit(Sender: TObject);
  60. begin
  61.   w2 := GroupBox2.Width / Form1.Width;
  62. end;
  63.  
  64. procedure TForm1.GroupBox3Exit(Sender: TObject);
  65. begin
  66.   w3 := GroupBox3.Width / Form1.Width;
  67. end;
  68.  
  69. end.
  70.  

If you want to use TSplitters you need to move the calculation of w1, w2 and w3 to the splitters OnMove-Event.

On the other hand, I found the OMultipanel component from Kluug a while ago when I had a similar problem.

http://www.kluug.net/omultipanel.php

I hope i could help  :)
Klaus.

wp

  • Hero Member
  • *****
  • Posts: 13207
Re: Resize multiple Controls proportionally to Form, when Form is maximized
« Reply #2 on: September 30, 2025, 12:49:18 pm »
You can try childsizing, a bit tricky though...
  • First "prepare" the groupboxes:
    • Drop a TBevel in each groupbox
    • Set its Shape to bsBox so that it is not visible
    • Set its Width to the width of the groupbox in the unscaled case, i.e. 250, 400 and 600 px, respectively.
    • Set AutoSize of the groupboxes to true.
    • I did not check this, but when the groupboxes already are autosized the placeholder bevel is not required.
  • Go to the ChildSizing properties of the groupboxes parent and make the following adjustments:
    • ControlsPerLine = 3 (because there are 3 groupboxes)
    • EnlargeHorizontal = crsScaleChilds, ShrinkHorizontal = crsScaleChilds
    • EnlargeVertical and ShrinkVertical: find suitable settings, i.e., keep crsAnchorAligning to keep the groupbox Autosized height, or use any other setting to expand the groupboxes over the height of their container.
    • Layout=cclLeftToRightThenTopToBottom --> the groupboxes should fill the width and height of their container. When the form is resized the groupboxes should keep their width ratio.
    • Define the distance between the groupboxes in HorizontalSpacing, and the margin by LeftRightSpacing and TopBottomSpacing
« Last Edit: September 30, 2025, 01:12:31 pm by wp »

Zvoni

  • Hero Member
  • *****
  • Posts: 3135
Re: Resize multiple Controls proportionally to Form, when Form is maximized
« Reply #3 on: September 30, 2025, 04:18:03 pm »
Thank you both.
I've implemented Klaus' idea, and it works for my requirements.
Solved
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018