Overload assignment operator of TPanel lazarus / 31-LCL ---------------------------------------------------------------------- chamfay · 2025-08-26 09:51 Hello everyone. This is my first post here, so I want to say thank you to all members. I need a panel with title so I made a custom TPanel class with TLabel and TFlowPanel inside it. 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 LPanel; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, StdCtrls, ExtCtrls, Graphics, Controls; type   { TLabeledPanel }   TLabeledPanel = class(TPanel)    FLabel: TLabel;    FFlowPanel: TFlowPanel;  public    constructor Create(TheOwner: TComponent); override;    destructor Destroy; override;  end; implementation { TLabeledPanel } constructor TLabeledPanel.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);   FLabel := TLabel.Create(self);  FLabel.Parent := self;  FLabel.Caption:='Panel title';  FLabel.Align:= alTop;  FLabel.Alignment:=taCenter;   FFlowPanel := TFlowPanel.Create(self);  FFlowPanel.Parent := self;  FFlowPanel.Align:= alClient;end; destructor TLabeledPanel.Destroy;begin  FLabel.Free;  FFlowPanel.Free;  inherited Destroy;end; end. 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 ulpanel; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation uses  LPanel;   {$R *.lfm}   { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  myPan: TLabeledPanel;  btn: TButton;  i: integer;begin  myPan := TLabeledPanel.Create(Self);  myPan.Width := 200;  myPan.Height := 200;  myPan.Left := 10;  myPan.top := 10;  myPan.Parent := Self;   for i := 0 to 10 do  begin    btn := TButton.Create(myPan);    btn.Parent := myPan.FFlowPanel;    btn.Caption:='Button ' + IntToStr(i);  end;end; end. Now I need to add some buttons in TFlowPanel outside TLabledPanel class for example I did: 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 btn.Parent := myPan.FFlowPanel; but I need to overload assignment operator of myPan to add buttons directly for example like this 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 btn.Parent := myPan; Thanks. ---------------------------------------------------------------------- jamie · 2025-08-26 21:24 The instant variable for your label control needs to be living in the TFORM1 so that you can gain access to it later on, instead of scanning the Components list of owned components, which you can btw.  Then in the Label class, you put a Procedure in the public section that deals with adding a control to it. Jamie ---------------------------------------------------------------------- chamfay · 2025-08-27 05:19 Hello. Maybe I don't understand you, but I need to use this TlabeledPanel many times (5 instances), and each instance with a label and many controls inside TFlowPanel; for that I defined a Label inside. BTW this is the class in my real program 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 LabledPanel; {$mode ObjFPC}{$H+} interface uses  Classes, ExtCtrls, StdCtrls, CommCtrl, Buttons, SysUtils, Controls, Graphics; type   { TLabledPanel }   TLabledPanel = class(TPanel)  private    FLabel: TLabel;    FFlowPanel: TFlowPanel;    procedure _CreateLabel(const aCaption: TCaption);    procedure _CreatePanel;    function GetCaption: TCaption;    procedure SetCaption(ACaption: TCaption);  public    constructor Create(TheOwner: TComponent; aCaption: TCaption); overload;    destructor Destroy; override;    property FlowPanel: TFlowPanel read FFlowPanel write FFlowPanel;    property Caption: TCaption read GetCaption write SetCaption;   end; implementation { TLabledPanel } procedure TLabledPanel._CreateLabel(const aCaption: TCaption);begin  FLabel := TLabel.Create(self);  FLabel.Parent := Self;  SetCaption(aCaption);  FLabel.Align := alTop;  FLabel.Font.Style := [fsBold, fsItalic];  FLabel.BorderSpacing.Top := 4;end; procedure TLabledPanel._CreatePanel;begin  FFlowPanel := TFlowPanel.Create(self);  FFlowPanel.Parent := self;  FFlowPanel.Align := alClient;  FFlowPanel.BevelOuter := bvNone;  FFlowPanel.BorderWidth := 2;end; function TLabledPanel.GetCaption: TCaption;begin  Result := FLabel.Caption;end; procedure TLabledPanel.SetCaption(ACaption: TCaption);begin  if FLabel.Caption = ACaption then Exit;  FLabel.Caption := '  ' + ACaption;end; constructor TLabledPanel.Create(TheOwner: TComponent; aCaption: TCaption);begin  inherited Create(TheOwner);   // init  Width := 300;  Height := 150;  BevelOuter := bvNone;  _CreateLabel(aCaption);  _CreatePanel; end; destructor TLabledPanel.Destroy;begin  FreeAndNil(FFlowPanel);  FreeAndNil(FLabel);  inherited Destroy;end; end. any advices please? Thanks. ---------------------------------------------------------------------- H₂SO₄ · 2025-08-27 05:38 Polymorphism (as provided by Pascal classes) doesn't mix well with assignment operator overloading in this case. When targeting the  Parent  property, the required operator would have to be  operator :=(ASelf: TLabeledPanel): TWinControl;  which is an impossible overload because it's an upcast. The necessity of invoking such an upcast operator is impossible to determine since in principle, subclasses already act equivalently to some superclass when invoking one of the latter's (non-virtual) methods. For example:  ControlAtPos()  is defined on  TWinControl.  When invoking it on a  TLabeledPanel  instance, should the compiler first call the custom assignment operator (which may in principle have all kinds of side effects, such as rearranging child controls) or should it call the method directly? With virtual methods, how should the compiler know whether to invoke the overridden or the inherited one? Apart from that, the existing code in the LCL has no way of knowing whether such a user-defined overload exists at all, making it impossible to be applied consistently. Quote from: Inheritance tree of TLabeledPanelTObject (+ IFPObserved) ┗━━ TPersistent (+ IUnknown, IInterfaceComponentReference)     ┗━━ TComponent         ┗━━ TLCLComponent             ┗━━ TControl                 ┗━━ TWinControl                     ┗━━ TCustomControl                         ┗━━ TCustomPanel                             ┗━━ TPanel                                 ┗━━ TLabeledPanel 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 Assert(Panel is TLabeledPanel);   // TrueAssert(Panel is TPanel);        // TrueAssert(Panel is TCustomPanel);    // TrueAssert(Panel is TCustomControl);  // TrueAssert(Panel is TWinControl);     // True{...} In my opinion, defining an opaque and somewhat confusing operator overload just to save 10 keystrokes isn't worth it. I encourage you to just make  FFlowPanel  publicly accessible - this is probably the simplest solution: 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 TLabeledPanel = class(TPanel)  FLabel: TLabel;  FFlowPanel: TFlowPanel;public  constructor Create(TheOwner: TComponent); override;  destructor Destroy; override;  property FlowPanel: TFlowPanel read FFlowPanel;end; ---------------------------------------------------------------------- Remy Lebeau · 2025-08-27 06:13 I would suggest exposing a public method to add a button, eg: 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 type  TLabeledPanel = class(TPanel)    ...  public    ...    procedure AddButton(ACaption: string {; other params as needed...});  end; ... procedure TLabeledPanel.AddButton(ACaption: string {; ...});var  btn: TButton;begin  btn := TButton.Create(FFlowPanel);  btn.Parent := FFlowPanel;  btn.Caption := ACaption;  ...end; 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 procedure TForm1.FormCreate(Sender: TObject);var  myPan: TLabeledPanel;  i: integer;begin  ...  for i := 0 to 10 do  begin    myPan.AddButton('Button ' + IntToStr(i) {,...});  end;end; ---------------------------------------------------------------------- chamfay · 2025-08-27 06:51 Quote from: Khrys on August 27, 2025, 07:38:26 amPolymorphism (as provided by Pascal classes) doesn't mix well with assignment operator overloading in this case. When targeting the  Parent  property, the required operator would have to be  operator :=(ASelf: TLabeledPanel): TWinControl;  which is an impossible overload because it's an upcast. The necessity of invoking such an upcast operator is impossible to determine since in principle, subclasses already act equivalently to some superclass when invoking one of the latter's (non-virtual) methods. For example:  ControlAtPos()  is defined on  TWinControl.  When invoking it on a  TLabeledPanel  instance, should the compiler first call the custom assignment operator (which may in principle have all kinds of side effects, such as rearranging child controls) or should it call the method directly? With virtual methods, how should the compiler know whether to invoke the overridden or the inherited one? Apart from that, the existing code in the LCL has no way of knowing whether such a user-defined overload exists at all, making it impossible to be applied consistently. Quote from: Inheritance tree of TLabeledPanelTObject (+ IFPObserved) ┗━━ TPersistent (+ IUnknown, IInterfaceComponentReference)     ┗━━ TComponent         ┗━━ TLCLComponent             ┗━━ TControl                 ┗━━ TWinControl                     ┗━━ TCustomControl                         ┗━━ TCustomPanel                             ┗━━ TPanel                                 ┗━━ TLabeledPanel 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 Assert(Panel is TLabeledPanel);   // TrueAssert(Panel is TPanel);        // TrueAssert(Panel is TCustomPanel);    // TrueAssert(Panel is TCustomControl);  // TrueAssert(Panel is TWinControl);     // True{...} In my opinion, defining an opaque and somewhat confusing operator overload just to save 10 keystrokes isn't worth it. I encourage you to just make  FFlowPanel  publicly accessible - this is probably the simplest solution: 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 TLabeledPanel = class(TPanel)  FLabel: TLabel;  FFlowPanel: TFlowPanel;public  constructor Create(TheOwner: TComponent); override;  destructor Destroy; override;  property FlowPanel: TFlowPanel read FFlowPanel;end; Very helpful answer.  Since it's impossible so I will follow your advice. Regards. ---------------------------------------------------------------------- chamfay · 2025-08-27 07:01 Quote from: Remy Lebeau on August 27, 2025, 08:13:51 amI would suggest exposing a public method to add a button, eg: 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 type  TLabeledPanel = class(TPanel)    ...  public    ...    procedure AddButton(ACaption: string {; other params as needed...});  end; ... procedure TLabeledPanel.AddButton(ACaption: string {; ...});var  btn: TButton;begin  btn := TButton.Create(FFlowPanel);  btn.Parent := FFlowPanel;  btn.Caption := ACaption;  ...end; 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 procedure TForm1.FormCreate(Sender: TObject);var  myPan: TLabeledPanel;  i: integer;begin  ...  for i := 0 to 10 do  begin    myPan.AddButton('Button ' + IntToStr(i) {,...});  end;end; Good suggestion, also nice and simple function to pass many parameters at once. Thanks. ---------------------------------------------------------------------- chamfay · 2025-08-27 10:00 Quote from: Khrys on August 27, 2025, 07:38:26 am Quote from: Inheritance tree of TLabeledPanelTObject (+ IFPObserved) ┗━━ TPersistent (+ IUnknown, IInterfaceComponentReference)     ┗━━ TComponent         ┗━━ TLCLComponent             ┗━━ TControl                 ┗━━ TWinControl                     ┗━━ TCustomControl                         ┗━━ TCustomPanel                             ┗━━ TPanel                                 ┗━━ TLabeledPanel How do I get like this tree? ---------------------------------------------------------------------- cdbc · 2025-08-27 14:22 Hi Walk backwards up the object tree, putting the objects in a stack. Now, pop the entire stack and write the individual items with increasing indentation to a TMemo... Something like this: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 procedure TfrmMain.btnWalkClick(Sender: TObject);var O: TClass;begin  fStackObj:= CreStack;  O:= lblVers.ClassType;  repeat  fStackObj.Push(O);    Memo1.Append(O.ClassName);    O:= O.ClassParent;  until O = nil;  Memo1.Append(format('-> Stack count: %d',[fStackObj.Count]));end; procedure TfrmMain.btnTreeClick(Sender: TObject);const indnt = '  ';var li: integer = 0;  function Indent: string;var l: byte; begin Result:= ''; for l:= 0 to li-1 do Result+= indnt; end;begin  if (Assigned(fStackObj) and (fStackObj.Count > 0)) then begin    while not fStackObj.IsEmpty do begin      Memo1.Append(Indent+tclass(fStackObj.Pop).ClassName);      inc(li);  end;  end;end;The above snippet uses 'icontainers' + friends, but any stack will do... Regards Benny ---------------------------------------------------------------------- chamfay · 2025-08-27 21:52 Quote from: cdbc on August 27, 2025, 04:22:09 pmHi Walk backwards up the object tree, putting the objects in a stack. ....... Nice, thank you