Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Component Help

Posted on 2001-08-14
6
176 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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