Recent

Author Topic: [Solved] I need some help writing a new component.  (Read 4826 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
[Solved] I need some help writing a new component.
« on: August 21, 2011, 10:30:05 am »
I need some help writing a new component.  What I have does what I need it to except that Caption:=''; does not clear the TPanel.Caption when it is created.  I tried using TLabeledEdit but it is too dangerous.  Clicking on the Label does not select the component.  It selects the component behind it.  Two times now I have accidentally deleted a TPanel containing many components and it can not be undone.  You simply have to rebuild it.  Also TLebeledEdit does not respect BiDiMode.  The component is not complete, but there is no point in going further until I have a solid design.

Basically I am using a TPanel as a container for a TLabel and a TEdit.  I think that TPanel is not the best choice for a container.  Any suggestions or code improvements would be very much appreciated. 

type
  TAviLabeledEdit = class(TPanel)
  private
    FLabelSpacing: Integer;
    FEmbeddedLabel: TLabel;
    FEmbeddedEdit: TEdit;
    procedure SetLabelSpacing(const Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing default 3;
    property AEdit: TEdit read FEmbeddedEdit;
    property ALabel: TLabel read FEmbeddedLabel;
  end;

{---}

constructor TAviLabeledEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FEmbeddedLabel:= TLabel.Create(Self);
  FEmbeddedLabel.Parent:= Self;       
  FEmbeddedLabel.SetSubComponent(true); 
  FEmbeddedLabel.Align:= alTop;
  FEmbeddedLabel.Layout:= tlCenter;
  FEmbeddedLabel.Name:= 'AviLabel';
  FEmbeddedLabel.Caption:= FEmbeddedLabel.Name;

  FEmbeddedEdit:= TEdit.Create(Self);
  FEmbeddedEdit.Parent:= Self;
  FEmbeddedEdit.SetSubComponent(true);
  FEmbeddedEdit.Align:= alBottom;
  FEmbeddedEdit.Name:= 'AviEdit';
  FEmbeddedEdit.Text:= '';

  FEmbeddedLabel.ControlStyle:= FEmbeddedLabel.ControlStyle - [csNoDesignSelectable];
  FEmbeddedEdit.ControlStyle:= FEmbeddedEdit.ControlStyle - [csNoDesignSelectable];

  Caption:= '';
  BevelOuter:= bvNone;
  LabelSpacing:= 1;
  Height:= FEmbeddedLabel.Height+FEmbeddedEdit.Height+LabelSpacing;
  Width:= 100;
end;

procedure TAviLabeledEdit.SetLabelSpacing(const Value: Integer);
begin
  if Value=FLabelSpacing then exit;
  FLabelSpacing:=Value;
  Height:= FEmbeddedLabel.Height+FEmbeddedEdit.Height+FLabelSpacing;
end;
« Last Edit: August 22, 2011, 09:58:06 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 612
Re: I need some help writing a new component.
« Reply #1 on: August 22, 2011, 07:12:39 am »
Hello Avishai:

I'm very new to Lazarus (v. 0.9.30), running on Windows XP.

I set up a small test program using your code here, declaring a variable of type TAviLabeledEdit in my main unit and creating an instance of it in my FormCreate event.  I don't see any label on the panel that is created -- it looks fine.

I didn't try to create an actual component though and add it to a package.  Is that where you run into the problem?

If so, I'd suggest you try overriding the Loaded procedure for your component, and putting your caption := ''
statement in that procedure, rather than in your constructor.  Strictly a guess, but I bet right now, you're constructor is trying to clear the caption, but your component when at load time is using the panel's default caption.

Again though, I'm very new to Lazarus.


Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: I need some help writing a new component.
« Reply #2 on: August 22, 2011, 08:27:33 am »
Thank you Curt for the suggestion.  I'll try moving Caption:='' as you suggest and let you know how it works out.  And welcome to the world of Lazarus.  These are a great bunch of people and always so willing to help.  Enjoy!  I suggested a while back that they change the word "Members" to "Citizens" :D
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: I need some help writing a new component.
« Reply #3 on: August 22, 2011, 09:25:27 am »
Curt, I tried your suggestion and a few other things without success.  I still have to manually set the Caption:='';

I don't think TPanel is the best choice for the container and wanted to use TCustomControl from the start, but couldn't get it to compile/install. 
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: [Solved] I need some help writing a new component.
« Reply #4 on: August 22, 2011, 10:06:14 am »
 I finally got it to work as a TCustomControl.  Now all I have to do is add the bells and whistles.

type
  TAviLabeledEdit = class(TCustomControl)
  private
    FLabelSpacing: Integer;
    FEmbeddedLabel: TLabel;
    FEmbeddedEdit: TEdit;
    procedure SetLabelSpacing(const Value: Integer);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property AEdit: TEdit read FEmbeddedEdit;
    property ALabel: TLabel read FEmbeddedLabel;
    property BiDiMode;
    property LabelSpacing: Integer read FLabelSpacing write SetLabelSpacing default 3;
    property ParentBiDiMode;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Avi Controls',[TAviLabeledEdit]);
end;

constructor TAviLabeledEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FEmbeddedLabel:= TLabel.Create(Self);
  FEmbeddedLabel.Parent:= Self;     
  FEmbeddedLabel.SetSubComponent(true);
  FEmbeddedLabel.Align:= alTop;
  FEmbeddedLabel.Layout:= tlCenter;
  FEmbeddedLabel.Name:= 'AviLabel';
  FEmbeddedLabel.Caption:= FEmbeddedLabel.Name;

  FEmbeddedLabel.ControlStyle:= FEmbeddedLabel.ControlStyle - [csNoDesignSelectable];

  FEmbeddedEdit:= TEdit.Create(Self);
  FEmbeddedEdit.Parent:= Self;
  FEmbeddedEdit.SetSubComponent(true);
  FEmbeddedEdit.Align:= alBottom;
  FEmbeddedEdit.Name:= 'AviEdit';
  FEmbeddedEdit.Text:= '';

  FEmbeddedEdit.ControlStyle:= FEmbeddedEdit.ControlStyle - [csNoDesignSelectable];

  LabelSpacing:= 1;
  Height:= FEmbeddedLabel.Height+FEmbeddedEdit.Height+LabelSpacing;
  Width:= 100;
end;

procedure TAviLabeledEdit.SetLabelSpacing(const Value: Integer);
begin
  if Value=FLabelSpacing then exit;
  FLabelSpacing:=Value;
  Height:= FEmbeddedLabel.Height+FEmbeddedEdit.Height+FLabelSpacing;
end;
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018