Recent

Author Topic: Container like TPanel which resizes itself to match its child controls?  (Read 2587 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Is there some container like TPanel which resizes itself to match its child controls?

If the child controls are stacked and resized either vertically or horiziontally the container expands to match the new size?

Of course the children will have to be aligned ie alTop,alClient etc, but enlarging one of them will push those below or to the side of it push against the containser and enlarge it, and reducing them will also reduce the seize of the container.
Lazarus 3.0/FPC 3.2.2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #1 on: February 05, 2024, 09:52:14 pm »
Of course the children will have to be aligned ie alTop,alClient etc, but enlarging one of them will push those below or to the side of it push against the containser and enlarge it, and reducing them will also reduce the seize of the container.
I do assume that any kind of alignment will make the aligned control slave to the parent.
alTop/alBottom = you loose control over Width property
alLeft/alRight = you loose control over Heigth property
alClient = you loose control over all properties.

I hope it helps on your journey.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

rvk

  • Hero Member
  • *****
  • Posts: 6748
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #2 on: February 05, 2024, 09:58:22 pm »
A TScrollBox with the TScrollBox.AutoSize set to true maybe?
(Didn't test this.)

https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tscrollbox.html

Thaddy

  • Hero Member
  • *****
  • Posts: 17136
  • Ceterum censeo Trump esse delendam
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #3 on: February 05, 2024, 10:08:32 pm »
He wants the children controlling the parent. Happens all the time... O:-)
Where is the logic in that?
The closest you can get is proper anchoring or messages to notify the parent that you have resized/moved.

But really?  Children controlling the parent? As opposed to real-life this can actually be done. Silly, but can. Get back to this tomorrow. It won't be nice and will draw in a lot of comments....
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

rvk

  • Hero Member
  • *****
  • Posts: 6748
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #4 on: February 05, 2024, 10:10:37 pm »
The closest you can get is proper anchoring or messages to notify the parent that you have resized/moved.
Isn't that what happens with TScrollBox.AutoSize := true ?

But yeah, I also don't see any logic in this  ;)

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #5 on: February 05, 2024, 11:31:47 pm »
TPanel has an autosize, that does not work?
The only true wisdom is knowing you know nothing

Joanna

  • Hero Member
  • *****
  • Posts: 1428
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #6 on: February 06, 2024, 12:23:10 am »
Try tflowpanel instead of tpanel
You can also constrain the flowpanel to fit the child controls.
« Last Edit: February 06, 2024, 12:26:13 am by Joanna »
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #7 on: February 06, 2024, 12:35:39 am »
I wonder what happen if you set within a container some different alignments and then use autosize/autofit ... funny things I guess lol

I do not want to see the parent window message log how several child controls continue fight each other about resizing :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6953
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #8 on: February 06, 2024, 12:50:30 am »
I guess you should try it.

The child controls don't move around with a TPanel. The Flow panel does.
The only true wisdom is knowing you know nothing

Joanna

  • Hero Member
  • *****
  • Posts: 1428
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #9 on: February 06, 2024, 01:39:09 am »
Depending upon the number of child controls, you can either align flowpanel to client or constrain it to fit the total width of child controls. I haven’t tried autosize with flowpanel though
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #10 on: February 06, 2024, 07:59:29 am »
Truth is I've never tried TPanel and TScrollbox autosize in relation to the size of their contents. I don't think I ever thought of them in terms of the size of their child controls.

I've always thought of Autosize in relation to controls with labels or text expanding to fit them.

Now I'm off to find means of resizing and moving objects at runtime.
Lazarus 3.0/FPC 3.2.2

Joanna

  • Hero Member
  • *****
  • Posts: 1428
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #11 on: February 06, 2024, 12:52:24 pm »
Flowpanel can rearrange controls at runtime automatically. I don’t know of any other control that can do that. Many people don’t bother with resizing things they just use scrollbox or assume the form will always be large size.
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

rvk

  • Hero Member
  • *****
  • Posts: 6748
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #12 on: February 06, 2024, 01:04:51 pm »
Flowpanel can rearrange controls at runtime automatically. I don’t know of any other control that can do that. Many people don’t bother with resizing things they just use scrollbox or assume the form will always be large size.
TPanel with AutoSize also just resizes when it's children resize.

The problem with TFlowPanel is that you can't put your component on specific positions. It will always snap to align with the other components. With TPanel you have more freedom regarding positioning.

To test it:
Create new project.
Put a TPanel on the form (make it almost as large as the form)
Put a TButton on the TPanel on the richt-lower side
Put a TMemo on the TPanel on the left-upper side
Make TPanel AutoSize = true
(You see that TPanel snaps to accommodate the components)

Copy and paste the code below completely in and execute.
You can now resize the TMemo with dragging the right-lower corner.
You'll see that TPanel also resizes perfectly.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TSizeableMemo = class(TMemo)
  15.    private
  16.      FDragging: Boolean;
  17.      FLastPos: TPoint;
  18.    protected
  19.      procedure MouseDown(Button: TMouseButton; Shift:
  20.        TShiftState; X, Y: Integer); override;
  21.      procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  22.      procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
  23.        X, Y: Integer); override;
  24.    end;
  25.  
  26.   TMemo = class(TSizeableMemo); // here we just overrule the current TMemo with our own SizeableMemo ;)
  27.  
  28.   TForm1 = class(TForm)
  29.     Button1: TButton;
  30.     Memo2: TMemo;
  31.     Panel1: TPanel;
  32.   private
  33.  
  34.   public
  35.  
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. procedure TSizeableMemo.MouseDown(Button: TMouseButton;
  46.   Shift: TShiftState; X, Y: Integer);
  47. begin
  48.   if (Button = mbLeft) and ((Width - x ) < 10) and
  49.     ((Height - y ) < 10) then
  50.   begin
  51.     FDragging := TRue;
  52.     FLastPos := Point(x, y);
  53.     MouseCapture := true;
  54.     Screen.cursor := crSizeNWSE;
  55.   end
  56.   else
  57.     inherited;
  58. end;
  59.  
  60. procedure TSizeableMemo.MouseMove(Shift: TShiftState; X, Y: Integer);
  61. var
  62.   r: TRect;
  63. begin
  64.   if FDragging then
  65.   begin
  66.     r := BoundsRect;
  67.     SetBounds( r.left, r.top, r.right - r.left + X - FlastPos.X,
  68.     r.bottom - r.top + Y - Flastpos.Y );
  69.     FLastPos := Point( x, y );
  70.   end
  71.   else
  72.   begin
  73.     inherited;
  74.     if ((Width - x ) < 10) and ((Height - y ) < 10) then
  75.       Cursor := crSizeNWSE
  76.     else
  77.       Cursor := crDefault;
  78.   end;
  79. end;
  80.  
  81. procedure TSizeableMemo.MouseUp(Button: TMouseButton;
  82.   Shift: TShiftState; X, Y: Integer);
  83. begin
  84.   if FDragging then
  85.   begin
  86.     FDragging := False;
  87.     MouseCapture := false;
  88.     Screen.Cursor := crDefault;
  89.   end
  90.   else
  91.     inherited;
  92. end;
  93.  
  94. end.
  95.  

Joanna

  • Hero Member
  • *****
  • Posts: 1428
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #13 on: February 06, 2024, 01:34:35 pm »
One thing I’ve noticed with flowpanel is that the anchoring might not work for controls inside it. I was hoping to prevent labels and trackbar together by anchoring to neighbor but it doesn’t seem to work
✨ 🙋🏻‍♀️ More Pascal enthusiasts are needed on IRC .. https://libera.chat/guides/ IRC.LIBERA.CHAT  Ports [6667 plaintext ] or [6697 secure] channel #fpc  #pascal Please private Message me if you have any questions or need assistance. 💁🏻‍♀️

rvk

  • Hero Member
  • *****
  • Posts: 6748
Re: Container like TPanel which resizes itself to match its child controls?
« Reply #14 on: February 06, 2024, 01:37:51 pm »
One thing I’ve noticed with flowpanel is that the anchoring might not work for controls inside it. I was hoping to prevent labels and trackbar together by anchoring to neighbor but it doesn’t seem to work
No, with the TFlowPanel you have a lot less control about position inside the panel.

On the other hand... if the TFlowPanel itself resizes (because the screen resizes / rotates for instance) then the flowpanel will flow the containing components in a correct state.

See for example https://wiki.freepascal.org/TFlowPanel

But when that is not needed and you want to position the label/button/memo yourself, TPanel would be more suitable.

 

TinyPortal © 2005-2018