Recent

Author Topic: Make custom components be visible on TForm (newbie)  (Read 6569 times)

ezlage

  • Guest
Make custom components be visible on TForm (newbie)
« on: August 26, 2015, 12:57:12 am »
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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #1 on: August 26, 2015, 01:56:19 am »
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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #2 on: August 26, 2015, 07:56:58 pm »
This looks boring. But I will try!

Thank you!

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Make custom components be visible on TForm (newbie)
« Reply #3 on: August 26, 2015, 10:10:49 pm »
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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ezlage

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #4 on: August 27, 2015, 05:30:50 pm »
Thank you, taaz.

I will try like you suggested.

ezlage

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #5 on: August 28, 2015, 07:30:01 pm »
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?
« Last Edit: August 28, 2015, 07:38:21 pm by ezlage »

fabiopesaju

  • Jr. Member
  • **
  • Posts: 93
Re: Make custom components be visible on TForm (newbie)
« Reply #6 on: August 28, 2015, 07:42:14 pm »
it is a silly question but you cleared the components in destroy method?

ezlage

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #7 on: August 29, 2015, 01:41:58 am »
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

  • Sr. Member
  • ****
  • Posts: 309
Re: Make custom components be visible on TForm (newbie)
« Reply #8 on: August 29, 2015, 02:43:05 am »
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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #9 on: August 29, 2015, 03:18:49 am »
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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #10 on: August 29, 2015, 03:58:41 am »
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

  • Sr. Member
  • ****
  • Posts: 309
Re: Make custom components be visible on TForm (newbie)
« Reply #11 on: August 29, 2015, 05:02:46 am »
So, 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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #12 on: August 29, 2015, 06:13:19 am »
So, 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

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #13 on: August 29, 2015, 07:06:52 am »
You still need Parent := Self, and you wont see TLabel's if they dont have captions.

Code: Pascal  [Select][+][-]
  1. unit SCLabeledEdit;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Controls, Dialogs, ExtCtrls, Forms, Graphics, LResources, StdCtrls;
  9.  
  10. type
  11.  
  12.   TSCLabeledEdit = class(TPanel)
  13.   private
  14.   protected
  15.     FLabelTop, FLabelBottom: TLabel;
  16.     FEdit: TEdit;
  17.   published
  18.     property TopLabel: TLabel read FLabelTop;
  19.     property BottomLabel: TLabel read FLabelBottom;
  20.     property Edit: TEdit read FEdit;
  21.   public
  22.     constructor Create(AOwner: TComponent); override;
  23.   end;
  24.  
  25. procedure Register;
  26.  
  27. implementation
  28.  
  29. procedure Register;
  30. begin
  31.   RegisterComponents('SemperCOM', [TSCLabeledEdit]);
  32. end;
  33.  
  34. constructor TSCLabeledEdit.Create(AOwner: TComponent);
  35. begin
  36.   inherited Create(AOwner);
  37.   with GetControlClassDefaultSize do begin
  38.     SetInitialBounds(0, 0, CX, CY);
  39.   end;
  40.   FLabelBottom := TLabel.Create(Self);
  41.   FLabelBottom.Caption := 'Bottom Label';
  42.   FLabelBottom.Align := alTop;
  43.   FLabelBottom.Parent := Self;
  44.   FLabelBottom.SetSubComponent(True);
  45.  
  46.   FEdit := TEdit.Create(Self);
  47.   FEdit.SetSubComponent(True);
  48.   FEdit.Align := alTop;
  49.   FEdit.Parent := Self;
  50.  
  51.   FLabelTop := TLabel.Create(Self);
  52.   FLabelTop.Caption := 'Top Label';
  53.   FLabelTop.Parent := Self;
  54.   FLabelTop.Align := alTop;
  55.   FLabelTop.SetSubComponent(True);
  56. end;
  57.  
  58. end.
  59.  
« Last Edit: October 02, 2015, 03:20:53 am by Geepster »

derek.john.evans

  • Guest
Re: Make custom components be visible on TForm (newbie)
« Reply #14 on: August 29, 2015, 07:09:37 am »
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.

 

TinyPortal © 2005-2018