Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Property to control 1

Status
Not open for further replies.

browneye

Programmer
Nov 21, 2000
82
US
Hi,
I am trying to add one property to a label. I dont get an error but i cant see "FieldName" property either. Any Idea what i am doing wrong? Heres code


Thanks in advance

unit Form1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
//lblID: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
local_bDataChanged: Boolean;
procedure SetDataChanged(Value: Boolean) ;
public
{ Public declarations }
property bDataChanged : Boolean read local_bDataChanged write SetDataChanged;
end;

//Label
type
TlblID = class(TLabel)
private
local_FieldName: string;
procedure SetFieldName(Value: String) ;
public
property FieldName : String read local_FieldName write SetFieldName;
end;




var
Form1: TForm1;
lblID: TLabel;
//LblID: Form1.LblID;

implementation

{$R *.dfm}


procedure TForm1.SetDataChanged(Value: Boolean);
begin
local_bDataChanged := Value;
end;


procedure TlblID.SetFieldName(Value: String);
begin
local_FieldName := Value;
end;


end.
 
Only Published properties appear in the Object Inspector. I presume you've properly registered the component. If you've just added the TlblID type code to your form unit, throw it away and start again. The Delphi help file has step-by-step instructions on Writing Components.
 
Griffyn is right, by looking at what you are trying to achieve the best thing to do is to create a new component that inherits from TLabel and add your property "FieldName" to it.

However, looking at your commented code it seems to me you also need to be aware of the following:


1. You probably dont know that delphi allows forward declarations
A forward declaration has the form

Code:
   TMyClass = class;

A forward declaration written as "TYourClass=class;" is
basically a way to say to the compiler "I Promise there will be class of type TYourClass down below in my code" (so the compiler will not complain if you use it so long as you keep your promise)


2. In your code you have two type sections
You can define all your classes under one type section

Code:
   type
      TMyClass1=class
       ...
      end;
     
      TMyClass2=class
      ...
      end;


3. To see the new property "FieldName" you need to define a variable of type TLblId
In your code there is no variable of type TLblID. You need to define one.
Code:
      lblID



The following repetition of your code may clarify a little more


Code:
unit Form1;

...
type
  
  //Forward declaration of TLblID class
  TLblID=class;

  TForm1 = class(TForm)
   
    //TLblId has been forward declared therefore
    //you can define variables of type TLblID 
    //if you want to  
    lblID: TLblID; 
    ...
   
  end;

  //type  (you had this uncomented) 
  //Dont need to specify type again you can define as 
  //many classes as you want under one type section 

  //Now define TLblId class to fulfill the forward promise
  TlblID = class(TLabel)
  private
      local_FieldName: string;
      procedure SetFieldName(Value: String) ;
  public
       property FieldName : String read local_FieldName write SetFieldName;
  end;



var
  Form1: TForm1;
  lblID: TLblID; //you can define variables of lblID if you want 


implementation
...

end.


I hope this helps you a little more.
 
Bledazemi I am new to delphi. WOW!! I appriciate your detailed suggestions. Griffyn is right too..but if checkout help there is simillar kinda example. It means it works . I am just curious why I am getting an error. Still I get an [Error] Unit1.pas(10): E2086 Type 'TLblID' is not yet completely defined

I am just trying add one property to form as well as label.
Do you think I need to use TComponent? Dont know how to use it either :)

Thanks your help is appriciated!!

Here is code

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type //Form
TLblID = class;
TForm1 = class(TForm)
private
local_bDataChanged: Boolean;
public
property bDataChanged : Boolean read local_bDataChanged write local_bDataChanged;
end;//If I remove this I get an error E2169 Field Definition not allowed after methods

type //If I remove this I get an error E2169 Field Definition not allowed after methods
TLBLID = class(TLabel)
private
local_FieldName: string;
public
property FieldName : String read local_FieldName write local_FieldName;
end;

var
Form1: TForm1;
LBLID: TLBLID;

implementation

{$R *.dfm}

end.
 
The error "E2086 Type 'TLblID' is not yet completely defined" happens because you have two "type" sections you have typed the word "type" twice in your unit. You only need one type section.

Code:
...
type

   TLblID=class;

   TForm1 = class(TForm) 
   ...
   end;

//dont write the word "type" again 
   
   TLblID=class(TLabel)
   ...
   end;

 
It's not so much that you can't have two "type" sections (you can). But you must complete the definition of any forward declarations before starting a new "type" section.

But I'm curious. Why are you wanting to add a new property to a TLabel? I've never had the urge to do so. What's the purpose?

If you really need to have something unique for each of your labels, you can use the Tag property. If a simple number isn't good enough, you can use the Tag property to store a pointer to any kind of object you care to create.

Since you say you are new to Delphi, the best advice I can give you is "keep it simple" and modifying Delphi components is not simple.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top