Recent

Author Topic: TCustomControl descendant not resizeable in Designer  (Read 3680 times)

howardpc

  • Hero Member
  • *****
  • Posts: 3972
TCustomControl descendant not resizeable in Designer
« on: November 09, 2021, 05:41:50 pm »
I have a TCustomControl component which parents a TWincontrol and a TLabel. It autosizes via an overridden CalculatePreferredSize, behaving as I want when instantiated in code.
However, when installed on the component palette and dropped on a form it is selectable, but cannot be resized. It presumably uses the default Lazarus component editor.
Any ideas how to get it to resize in the Designer?

wp

  • Hero Member
  • *****
  • Posts: 9571
Re: TCustomControl descendant not resizeable in Designer
« Reply #1 on: November 09, 2021, 06:20:31 pm »
Just to make sure: AutoSize is off when you attempt to resize the control?

How did you position the two controls to each other? Using anchoring is sometimes tricky and can lead to locked situations. In these cases, usage of ChildSizing is easier.

The attached quick-and-dirty unit combines a TLabel and a TEdit, and here resizing works correctly when AutoSize is false:

Code: Pascal  [Select][+][-]
  1. unit combined;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Controls, StdCtrls;
  9.  
  10. type
  11.   TCombinedEdit = class(TCustomControl)
  12.   private
  13.     FLabel: TLabel;
  14.     FEdit: TEdit;
  15.     function GetCaption: String;
  16.     function GetText: String;
  17.     procedure SetCaption(const AValue: String);
  18.     procedure SetText(const AValue: String);
  19.   protected
  20.     procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
  21.       WithThemeSpace: Boolean); override;
  22.   public
  23.     constructor Create(AOwner: TComponent); override;
  24.   published
  25.     property Caption: String read GetCaption write SetCaption;
  26.     property Text: String read GetText write SetText;
  27.     property AutoSize default true;
  28.   end;
  29.  
  30. procedure Register;
  31.  
  32. implementation
  33.  
  34. procedure Register;
  35. begin
  36.   RegisterComponents('Misc', [TCombinedEdit]);
  37. end;
  38.  
  39.  
  40. constructor TCombinedEdit.Create(AOwner: TComponent);
  41. begin
  42.   inherited;
  43.   FLabel := TLabel.Create(self);
  44.   FLabel.Parent := self;
  45.   FLabel.BorderSpacing.Bottom := 2;
  46.   FLabel.WordWrap := true;
  47.   FLabel.AutoSize := true;
  48.   FLabel.Caption := 'Label';
  49.  
  50.   FEdit := TEdit.Create(self);
  51.   FEdit.Parent := self;
  52.  
  53.   Width := 200;
  54.   with ChildSizing do
  55.   begin
  56.     ControlsPerLine := 1;
  57.     EnlargeHorizontal := crsHomogenousChildResize;
  58.     Layout := cclLeftToRightThenTopToBottom;
  59.     VerticalSpacing := 2;
  60.   end;
  61.   AutoSize := true;
  62. end;
  63.  
  64. procedure TCombinedEdit.CalculatePreferredSize(
  65.   var PreferredWidth, PreferredHeight: integer; WithThemeSpace: Boolean);
  66. begin
  67.   PreferredHeight := FLabel.Top + FLabel.Height + FLabel.BorderSpacing.Bottom + FEdit.Height;
  68.   PreferredWidth := 0;
  69. end;
  70.  
  71. function TCombinedEdit.GetCaption: String;
  72. begin
  73.   Result := FLabel.Caption;
  74. end;
  75.  
  76. function TCombinedEdit.GetText: String;
  77. begin
  78.   Result := FEdit.Text;
  79. end;
  80.  
  81. procedure TCombinedEdit.SetCaption(const AValue: String);
  82. begin
  83.   FLabel.Caption := AValue;
  84.   Invalidate;
  85. end;
  86.  
  87. procedure TCombinedEdit.SetText(const AValue: String);
  88. begin
  89.   FEdit.Text := AValue;
  90.   Invalidate;
  91. end;
  92.  
  93. end.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

howardpc

  • Hero Member
  • *****
  • Posts: 3972
Re: TCustomControl descendant not resizeable in Designer
« Reply #2 on: November 10, 2021, 08:24:33 am »
Thanks, wp. You've put me on the right track with using childsizing rather than anchoring.
I'll share the outcome in due course, I hope.

 

TinyPortal © 2005-2018