Recent

Author Topic: Creating component with embedded TShape  (Read 2935 times)

vix

  • New Member
  • *
  • Posts: 27
Creating component with embedded TShape
« on: August 12, 2014, 09:09:19 pm »
Hello,
i try to create new component with a TShape fields attached. It inherits from TMemo and in fact it is a simple memo with shape located under, working as a colour indicator (you write some text and colour is changing, nevermind ;) ).

I have some start (with static shape placement on component create) but after component installation when i put my component on the form the tshape element is not visible. What can I do to make it be visible below my memo?

This is what i have now:
Code: [Select]
unit DMemo;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;

type

  { TDMemo }

  TDMemo = class(TMemo)
  private
    FShape: TShape;
  protected
    { Protected declarations }
  public
    constructor Create(AOwner: TComponent); override;
  published
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Experimental',[TDMemo]);
end;

{ TDMemo }

constructor TDMemo.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if FShape <> nil then Exit;

  FShape := TShape.Create(self);
  FShape.Visible:= Visible;

  FShape.Parent := Parent;
  FShape.Top:=10;
  FShape.Width:=100;
  FShape.Left:=10;
  FShape.Height:=20;
  FShape.Shape:= stRectangle;
  FShape.Pen.Style:= psSolid;
  FShape.Pen.Color:= clRed;
end;

end.

I work with Lazarus 1.2.4 and FPC 2.6.4 on Ubuntu 14.04.

Best!
Tarzan no understand what mean syntax error...
..look out for my english..

Bart

  • Hero Member
  • *****
  • Posts: 5667
    • Bart en Mariska's Webstek
Re: Creating component with embedded TShape
« Reply #1 on: August 12, 2014, 09:44:41 pm »
Override SetParent of your component to set the parent of the FShape as well?
Untested proposition b.t.w.

Code: [Select]
procedure TDMemo.SetParent(NewParent: TWinControl);
begin
  Inherited SetParent(NewParent);
  FShape.Parent := NewParent;
end;

Bart

vix

  • New Member
  • *
  • Posts: 27
Re: Creating component with embedded TShape
« Reply #2 on: August 12, 2014, 10:05:05 pm »
You are the man!
Didn't know about that. Now it works as expected, thank You very much :)

Best!
Tarzan no understand what mean syntax error...
..look out for my english..

 

TinyPortal © 2005-2018