Recent

Author Topic: Adding a new property for component  (Read 3338 times)

Elmug

  • Hero Member
  • *****
  • Posts: 849
Adding a new property for component
« on: August 05, 2011, 01:20:51 am »
Good people,

So far I have managed to use some basic components, with their properties as designed.

However, I have been wondering how difficult  (or simple) it would be to add to one or various components a new property. In its simplest way, perhaps to hold the value of a Real. Akin to the property Tag which can hold an integer.

I suppose it involves first creating a new type TagReal (or so), and when that new object type is declared, ...then what?

I am basically asking for a simple description of what I involves, in a few sentences, and hopefully some correct terminology by which I can search the web without going astray.

Or, some links to study the subject would also be appreciated.

And I don't mean create a new component, but rather add a property to an existing one, or copy an existing one to modify it with an extra property.

I do have the hunch that this may not be one thing to do. However, I have been very happy to be corrected in this forum. :)


Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Adding a new property for component
« Reply #1 on: August 05, 2011, 02:02:18 am »
Adding new property to existing component is easy. Example:
Code: [Select]
TMyLabel = class(TLabel)
  private
    FMyReal: Double;
  protected
  public
  end;       
In this case, I put FMyReal as private but you can set it as public or protected as well.
You can create this component in code like this:
Code: [Select]
var MyLabel: TMyLabel;
..........
MyLabel:=TMyLabel.Create(self);
  with MyLabel do
    begin
      Caption:='MyLabel';
      Left:=25;
      FMyReal:=3.14;
      Parent:=self;
    end;
Adding component to component palette and work with it in Object Inspector is somewhat more complicated, read wiki: http://wiki.lazarus.freepascal.org/How_To_Write_Lazarus_Component
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Adding a new property for component
« Reply #2 on: August 05, 2011, 02:34:18 am »
Code: [Select]
TMyLabel = class(TLabel)
  private
    FMyReal: Double;  // this is a Field, not a property
  protected
  public
    property MyReal :Double read FMyReal write FMyReal;
  end;     

Code: [Select]
var MyLabel: TMyLabel;
..........
MyLabel:=TMyLabel.Create(self);
  with MyLabel do
    begin
      Caption:='MyLabel';
      Left:=25;
      MyReal:=3.14;
      Parent:=self;
    end;

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Adding a new property for component
« Reply #3 on: August 05, 2011, 07:03:58 am »
Thank you so much, both much appreated.

What you explain, Blaazen, is kind of what I sensed would be much more of a challenge that I can not approach nor need to right now: adding things to the palette.

These knowledgeable simple solutions will avoid me from some code convolutions that teach a lot, but there are better ways often.

So, using what is described her is one thing I certainly will try out soon.

The code snippet you provide is very clear too, Typo. Thanks!
 :)

 

TinyPortal © 2005-2018