Solved

Component Help

Posted on 2001-08-14
6
177 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
Technology Partners: 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 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
How to debug For loops? 3 63
How to convert memory stream to PDF file 6 185
enhance the following code 3 42
Delphi android app hide keyboard 3 46
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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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