Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Component Help

Posted on 2001-08-14
6
Medium Priority
?
186 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
[X]
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
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
Slavak earned 200 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

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

609 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