Container like TPanel which resizes itself to match its child controls? lazarus / 31-LCL ---------------------------------------------------------------------- vfclists · 2024-02-05 20:42 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. ---------------------------------------------------------------------- KodeZwerg · 2024-02-05 20:52 Quote from: vfclists on February 05, 2024, 09:42:21 pmOf 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. ---------------------------------------------------------------------- rvk · 2024-02-05 20:58 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 · 2024-02-05 21:08 He wants the children controlling the parent. Happens all the time... 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.... ---------------------------------------------------------------------- rvk · 2024-02-05 21:10 Quote from: Thaddy on February 05, 2024, 10:08:32 pmThe 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 · 2024-02-05 22:31 TPanel has an autosize, that does not work? ---------------------------------------------------------------------- Joanna · 2024-02-05 23:23 Try tflowpanel instead of tpanel You can also constrain the flowpanel to fit the child controls. ---------------------------------------------------------------------- KodeZwerg · 2024-02-05 23:35 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 ---------------------------------------------------------------------- jamie · 2024-02-05 23:50 I guess you should try it. The child controls don't move around with a TPanel. The Flow panel does. ---------------------------------------------------------------------- Joanna · 2024-02-06 00:39 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 ---------------------------------------------------------------------- vfclists · 2024-02-06 06:59 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. ---------------------------------------------------------------------- Joanna · 2024-02-06 11:52 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. ---------------------------------------------------------------------- rvk · 2024-02-06 12:04 Quote from: Joanna on February 06, 2024, 12:52:24 pmFlowpanel 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TSizeableMemo = class(TMemo)   private     FDragging: Boolean;     FLastPos: TPoint;   protected     procedure MouseDown(Button: TMouseButton; Shift:       TShiftState; X, Y: Integer); override;     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;     procedure MouseUp(Button: TMouseButton; Shift: TShiftState;       X, Y: Integer); override;   end;   TMemo = class(TSizeableMemo); // here we just overrule the current TMemo with our own SizeableMemo ;)   TForm1 = class(TForm)    Button1: TButton;    Memo2: TMemo;    Panel1: TPanel;  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} procedure TSizeableMemo.MouseDown(Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  if (Button = mbLeft) and ((Width - x ) < 10) and  ((Height - y ) < 10) then  begin    FDragging := TRue;    FLastPos := Point(x, y);    MouseCapture := true;    Screen.cursor := crSizeNWSE;  end  else    inherited;end; procedure TSizeableMemo.MouseMove(Shift: TShiftState; X, Y: Integer);var  r: TRect;begin  if FDragging then  begin    r := BoundsRect;    SetBounds( r.left, r.top, r.right - r.left + X - FlastPos.X,    r.bottom - r.top + Y - Flastpos.Y );    FLastPos := Point( x, y );  end  else  begin    inherited;    if ((Width - x ) < 10) and ((Height - y ) < 10) then      Cursor := crSizeNWSE    else      Cursor := crDefault;  end;end; procedure TSizeableMemo.MouseUp(Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  if FDragging then  begin    FDragging := False;    MouseCapture := false;    Screen.Cursor := crDefault;  end  else    inherited;end; end. ---------------------------------------------------------------------- Joanna · 2024-02-06 12:34 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 ---------------------------------------------------------------------- rvk · 2024-02-06 12:37 Quote from: Joanna on February 06, 2024, 01:34:35 pmOne 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. ---------------------------------------------------------------------- Joanna · 2024-02-06 12:44 I might need to find better way to keep certain controls together. Maybe by putting them into a tpanel. Other than that , the flow panel works fairly well and I’m replacing most of my frames with tflowpanel descendants. ---------------------------------------------------------------------- rvk · 2024-02-06 12:49 Quote from: Joanna on February 06, 2024, 01:44:17 pmI might need to find better way to keep certain controls together. Maybe by putting them into a tpanel. Other than that , the flow panel works fairly well and I’m replacing most of my frames with tflowpanel descendants. Yes. TFlowPanel has a certain use-case for when the parent control changes in size (i.e. the flowpanel itself or the screen). Then it does a good job of rearranging the flow. You can also put a TPanel on a TFlowPanel if you want some space aroung a TLabel or Label/Input combo. The TPanel will flow in its parent naturally. It wasn't exactly what was asked by TS, namely a parent which will resize depending on its children (which TPanel.AutoSize does). The TFlowPanel rearranges its children depending on its own size. But yes... both have their own advantages. ---------------------------------------------------------------------- Joanna · 2024-02-06 13:41 I’ve never had much luck with autosize, maybe I’m doing something wrong. I ended up using the width of the text in controls to determine what size they should be. If I remember correctly, I once tried to use autosize on  a combobox but it didn’t size to fit widest item ---------------------------------------------------------------------- wp · 2024-02-06 14:34 Quote from: vfclists on February 05, 2024, 09:42:21 pmIs 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. What's wrong with TPanel? TGroupBox will work as well. Follow this description: Just drop any controls on a TPanel (or a TGroupBox). Do not touch their Align! Set the panel's AutoSize to true and the panel will change its size such that it closely contains the controls. you may notice that the top-most/left-most control has been moved into the upper left corner; if you want to change its position do this by adjusting its BorderSpacing.Left and BorderSpacing.Top parameters. Except for this top/left control, all other controls can be freely moved with the mouse, and the size of the panel adjusts when a control would reach beyond the current right/bottom bounds. Do not drag controls such that their left or top sides go out of the left/top borders of the panel (or at least do this very slowly); the control will stop at the left/top edges, but the opposite side of the panel will move quite quickly. This sounds wrong, maybe it is, but once you've accepted that nothing can be beyond the top/left edge of the top/left control you will not attempt to do so.