Dear all,
I want to combine TShape and TLabel to a new class:MyShape.
Two label can represent different things, such as
'Station 10' and 'Step 1'.
Hope I can get the result as attachment.
But for my code, it can only display a TShape, no TLable appear.
Here is my code, can anyone tell me where is wrong?
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
btnShape: TButton;
procedure btnShapeClick(Sender: TObject);
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
private
public
end;
type
MyShape = class(TShape)
private
stationName,stepName:TLabel;
public
property lblStation: TLabel read stationName write stationName;
property lblStep: TLabel read stepName write stepName;
constructor Create(TheOwner: TComponent);override;
procedure Paint; override;
end;
var
Form1: TForm1;
shape1:MyShape;
implementation
{$R *.lfm}
constructor MyShape.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
stationName:=TLabel.Create(TheOwner);
stepName:=TLabel.Create(TheOwner);
end;
procedure MyShape.Paint;
begin
inherited Paint;
stationName.Caption:='Station...';
stationName.Left:=0;
stationName.Top:=10;
stationName.Width:=50;
stationName.Height:=25;
stationName.Visible:=true;
stepName.Caption:='Step...';
stepName.Left:=0;
stepName.Top:=45;
stepName.Width:=50;
stepName.Height:=25;
stepName.Visible:=true;
end;
{ TForm1 }
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
shape1.Free;
end;
procedure TForm1.btnShapeClick(Sender: TObject);
begin
shape1:=MyShape.Create(Form1);
shape1.Shape:=TShapeType.stRectangle;
shape1.Left:=20;
shape1.Top:=130;
shape1.Width:=250;
shape1.Height:=130;
shape1.Parent:=Form1;
shape1.Visible:=true;
shape1.lblStation.Caption:='Station 10';
shape1.lblStep.Caption:='Step 1';
end;
end.
Best regards,
Jonvy