Make custom components be visible on TForm (newbie) lazarus / 31-LCL ---------------------------------------------------------------------- ezlage · 2015-08-25 22:57 Hi, friends! Another newbie question: I want to do something like this: Code: [Select]  TTest = class     ALabel: TLabel;     AField: TEdit;     AALabel: TLabel;     public       constructor Create(TheOwner: TComponent);       destructor Destroy;   end;   {...} How to make these TLabel and TEdit appears on my TForm? Sorry by my poor english. ---------------------------------------------------------------------- derek.john.evans · 2015-08-25 23:56 Creating custom controls is a large topic. There is a simple example here: http://wiki.freepascal.org/How_To_Write_Lazarus_Component Note: Scroll down to "Using embedded (visual) components" ---------------------------------------------------------------------- ezlage · 2015-08-26 17:56 This looks boring. But I will try! Thank you! ---------------------------------------------------------------------- taazz · 2015-08-26 20:10 why don't you make it easier for you then? Just use frames drop your controls in design time there and you can then place them anywhere you need them. ---------------------------------------------------------------------- ezlage · 2015-08-27 15:30 Thank you, taaz. I will try like you suggested. ---------------------------------------------------------------------- ezlage · 2015-08-28 17:30 Friends, I have aggregated the two ideas. I managed to create the LPK package and make visible subcomponents at designetime. But when I remove the component from form, is generated exception. Can anyone guide me to solve this? ---------------------------------------------------------------------- fabiopesaju · 2015-08-28 17:42 it is a silly question but you cleared the components in destroy method? ---------------------------------------------------------------------- ezlage · 2015-08-28 23:41 At design time, I can create my component on a form, but when I try to remove it, the access violation exception raises. What I did wrong? Code: [Select]unit SCLabeledEdit; {$mode objfpc}{$H+} interface uses   Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,   ExtCtrls; type   { TSCLabeledEdit }   TSCLabeledEdit = class(TPanel)   private   protected     leTopLabel, leBottomLabel: TLabel;   published     property TopLabel: TLabel read leTopLabel;     property BottomLabel: TLabel read leBottomLabel;   public     constructor Create(TheOwner: TComponent); override;     destructor Destroy; override;   end; procedure Register; implementation procedure Register; begin   RegisterComponents('SemperCOM',[TSCLabeledEdit]); end; { TSCLabeledEdit } constructor TSCLabeledEdit.Create(TheOwner: TComponent); begin   inherited Create(TheOwner);   Caption:=''; Text:='';   {with GetControlClassDefaultSize do     SetInitialBounds(0, 0, CX, CY);}   leTopLabel:=TLabel.Create(TheOwner);   with leTopLabel do begin     Parent:=Self;     SetSubComponent(True);   end;   leBottomLabel:=TLabel.Create(TheOwner);   with leBottomLabel do begin     Parent:=Self;     SetSubComponent(True);   end; end; destructor TSCLabeledEdit.Destroy; begin   leTopLabel.Free;   leBottomLabel.Free;   inherited Destroy; end; end. ---------------------------------------------------------------------- bylaardt · 2015-08-29 00:43 Parent:=self; and create(theOwner) of the same TComponent doesn't work. When you destroy a component,you destroy all subcomponents. but when say "self" is the new parent, you inform to component (letoplabel) must inform her parent (self) when "free" is acionated. And when TheOwner (panel) will free his subcomponents, he search for the component created (by .create) and he is gone! and then raise exception... ---------------------------------------------------------------------- ezlage · 2015-08-29 01:18 So, what changes I need to do on my code? I followed the steps on this link: http://wiki.freepascal.org/How_To_Write_Lazarus_Component ---------------------------------------------------------------------- derek.john.evans · 2015-08-29 01:58 Just a tip. Don't make design time components while developing. Creating a DTC package should be the very last thing you do after debugging your code. ---------------------------------------------------------------------- bylaardt · 2015-08-29 03:02 Quote from: ezlage on August 29, 2015, 03:18:49 amSo, what changes I need to do on my code? use: "create(self)" rather than "create(Theowner)"  for subcomponents and don't use parent:=object. ---------------------------------------------------------------------- ezlage · 2015-08-29 04:13 Quote from: bylaardt on August 29, 2015, 05:02:46 amQuote from: ezlage on August 29, 2015, 03:18:49 amSo, what changes I need to do on my code? use: "create(self)" rather than "create(Theowner)"  for subcomponents and don't use parent:=object. This way, the subcomponents aren't visible on the Panel. More suggestions? Sorry by my poor english. ---------------------------------------------------------------------- derek.john.evans · 2015-08-29 05:06 You still need Parent := Self, and you wont see TLabel's if they dont have captions. 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 SCLabeledEdit; {$mode objfpc}{$H+} interface uses  Classes, Controls, Dialogs, ExtCtrls, Forms, Graphics, LResources, StdCtrls; type   TSCLabeledEdit = class(TPanel)  private  protected    FLabelTop, FLabelBottom: TLabel;    FEdit: TEdit;  published    property TopLabel: TLabel read FLabelTop;    property BottomLabel: TLabel read FLabelBottom;    property Edit: TEdit read FEdit;  public    constructor Create(AOwner: TComponent); override;  end; procedure Register; implementation procedure Register;begin  RegisterComponents('SemperCOM', [TSCLabeledEdit]);end; constructor TSCLabeledEdit.Create(AOwner: TComponent);begin  inherited Create(AOwner);  with GetControlClassDefaultSize do begin    SetInitialBounds(0, 0, CX, CY);  end;  FLabelBottom := TLabel.Create(Self);  FLabelBottom.Caption := 'Bottom Label';  FLabelBottom.Align := alTop;  FLabelBottom.Parent := Self;  FLabelBottom.SetSubComponent(True);   FEdit := TEdit.Create(Self);  FEdit.SetSubComponent(True);  FEdit.Align := alTop;  FEdit.Parent := Self;   FLabelTop := TLabel.Create(Self);  FLabelTop.Caption := 'Top Label';  FLabelTop.Parent := Self;  FLabelTop.Align := alTop;  FLabelTop.SetSubComponent(True);end; end. ---------------------------------------------------------------------- derek.john.evans · 2015-08-29 05:09 O, yea. You don't need to destroy your sub control's, since they are components owned by the TPanel. ie: Destroying the TPanel will destroy all the components it owns. Hence the ownership concept.