Solved

Component Help

Posted on 2001-08-14
6
175 Views
Last Modified: 2010-04-06
I'm trying to learn to create components.

I want to create a Custom Version of TGroupBox with it's own TLabel.  Code below.

This version shows the TLabel property in the Object Inspector so I must be doing something right but as I try to use it in a program I get an Error similar to "Label not found"..

Please show me what I'm doing wrong.

Don

--------------------------------------------------------
unit S_GroupBox;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls;

type
  TS_GroupBox = class(TGroupBox)
  private
   FLabel: TLabel;
   procedure SetLabel(value: TLabel);
  protected
  public
   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;
  published
    property Label1: FLabel read FLabel write FLabel;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Spencer', [TS_GroupBox]);
end;

constructor TS_GroupBox.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 Label1 := TLabel.Create(AOwner);
End;

destructor TS_GroupBox.Destroy;
begin
 Label1.Free;
End;

procedure TS_GroupBox.SetLabel(value: TLabel);
begin
 if assigned(Value) then Label1.Assign(Value);
end;

END.
0
Comment
Question by:d32coder
6 Comments
 
LVL 3

Expert Comment

by:rondi
ID: 6383994
Your FormCreate should create FLabel instead of Label1:
  inherited Create(AOwner);
  FLabel := TLabel.Create(AOwner);

and in Destroy:
  FLabel.Free;

Remember that "Label1" is not the actual object reference,
it's just the property. Maybe your property should be:

  property Label1: TLabel read FLabel write SetLabel;


rondi.
0
 

Author Comment

by:d32coder
ID: 6384387
"Class TLabel not found"

I have StdCtrls in the uses clause...


0
 

Author Comment

by:d32coder
ID: 6384441
BTW, if it matters, I'm using Delphi 6
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 3

Accepted Solution

by:
Slavak earned 50 total points
ID: 6385003
First, missing call to inherited in destructor.
Second, I prefer following version of component :)

type
 TS_GroupBox = class(TGroupBox)
 private
  FLabel: TLabel;
  function GetLabel : String;
  procedure SetLabel(Value: String);
 protected
 public
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
 published
   property Label1: String read GetLabel write SetLabel;
 end;

procedure Register;

implementation

procedure Register;
begin
 RegisterComponents('Spencer', [TS_GroupBox]);
end;

constructor TS_GroupBox.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 Label1 := TLabel.Create(AOwner);
 With Label1 Do Begin
  Label1.Parent := Self;
  Label1.SetBounds(10, 10, Width, Height);
 End;

End;

destructor TS_GroupBox.Destroy;
begin
 Label1.Free;
 inherited;
End;

function TS_GroupBox.GetLabel : String;
Begin
 Result := Label1.Caption;
End;

procedure TS_GroupBox.SetLabel(Value: String);
begin
 Label1.Caption := Value;
end;
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6387423
Hi d32coder,

"Class TLabel not found" - you should register class TLabel.  You got this message regarding Delphi's streaming mechanism.

constructor TS_GroupBox.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 Label1 := TLabel.Create(AOwner); // (*)
End;

(*) creating label by this way you will get new instance of the label every time your component loaded.  

follow this link to see a sample about "one component inside other"

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20165727

-----
Igor.
0
 
LVL 3

Expert Comment

by:rondi
ID: 6388562
Change constructor to
  Label1 := TLabel.Create(Self);
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
Error E2158 compiling with Delphi XE10 Seattle 2 98
Convert a string into a TDateTime 5 60
DBGrid or StringGrid ? 6 88
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question