Solved

Component Help

Posted on 2001-08-14
6
173 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
Comment Utility
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
Comment Utility
"Class TLabel not found"

I have StdCtrls in the uses clause...


0
 

Author Comment

by:d32coder
Comment Utility
BTW, if it matters, I'm using Delphi 6
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 3

Accepted Solution

by:
Slavak earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Change constructor to
  Label1 := TLabel.Create(Self);
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now