Recent

Author Topic: Adding properties to controls  (Read 8728 times)

dahaiou

  • New Member
  • *
  • Posts: 40
Adding properties to controls
« on: August 03, 2012, 08:43:01 am »
I want to add some additional properties to a number of TEdit controls.

I guess the "clean" way to do it would be to declare a child class of TEdit and just declare the new variables I need. But I'm not sure how to make the IDE handle the new child class as a normal control (creating and placing it on a TForm etc.).

I am experimenting to see if I can avoid creating a child class, and use existing properties instead - such as an existing integer that is currently unused. Then I can use the integer value to point into an array to access any number of additional properties.

So, the question boils down to: What existing properties of a TEdit are "available" for my own custom use ie. I can put any value in them without causing problems.

After a few experiment runs I have come up with the following Integer properties, that don't seem to cause any problems and should be available to use the way I want (I will not be docking these controls or using help context):
 
Code: [Select]
    edX.LRDockWidth  := 4711;
    edX.TBDockHeight := 4712;
    edX.UndockHeight := 4713;
    edX.UndockWidth  := 4714;
    edX.HelpContext  := 4715;
Can anyone tell me, are these OK to use for my own needs or will there perhaps be undesired effects later that I haven't seen yet ? Which one would you use yourself - or can you suggest any other existing property to use for this ?

From a design point of view, what do you think of this approach?
Can you recommend a more elegant way of doing it?
« Last Edit: August 03, 2012, 08:45:52 am by dahaiou »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1088
    • formatio reticularis
Re: Adding properties to controls
« Reply #1 on: August 03, 2012, 08:59:36 am »
Why don't you use the TEdit's tag field? It is intended to store arbitrary values.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

howardpc

  • Hero Member
  • *****
  • Posts: 3361
Re: Adding properties to controls
« Reply #2 on: August 03, 2012, 09:28:28 am »
Why don't you use the TEdit's tag field? It is intended to store arbitrary values.
That only gives you one integer (Ptrint) to use.
The whole idea of reusing properties for unintended purposes is misguided. Using Tag is safe (it's provided for user customisation and is never touched by LCL code), but you can never be sure that touching other properties might not have unintended side-effects (unless you wrote the component yourself, and know intimate details of how it works).
It is straightforward to customise existing LCL components. Add the properties you need with apprpriate setter and getter methods and storage fields.
Then instantiate them 'by hand', giving an owner component (to handle later destruction), and set Parent and possibly Top and Left and any other defaults your component needs when initialized.
When you are satisfied that your customised component is debugged, add a RegisterComponent() call to stick it on the IDE Component Palette and save you having to do the setup code 'by hand' (look at any component's source to see how to do this). Here is a simple example:
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, StdCtrls;

type
  TEdX = class(TEdit)
  private
    FNewIntProp: integer;
    FNewStrProp: string;
    procedure SetNewIntProp(AValue: integer);
    procedure SetNewStrProp(AValue: string);
  public
    constructor Create(theComponent: TComponent); override;
    property NewIntProp: integer read FNewIntProp write SetNewIntProp;
    property NewStrProp: string read FNewStrProp write SetNewStrProp;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    edX: TEdX;
  public
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  edX:= TEdX.Create(Self);
  edX.Parent:= Self;
  edX.Caption:= IntToStr(edX.NewIntProp);
  Caption:= edX.NewStrProp;
end;

{$R *.lfm}

{ TEdX }

procedure TEdX.SetNewIntProp(AValue: integer);
begin
  if FNewIntProp=AValue then Exit;
  FNewIntProp:=AValue;
  // add custom code here
end;

procedure TEdX.SetNewStrProp(AValue: string);
begin
  if FNewStrProp=AValue then Exit;
  FNewStrProp:=AValue;
  // add custom code here
end;

constructor TEdX.Create(theComponent: TComponent);
begin
  inherited Create(theComponent);
  FNewStrProp:= 'new string property value';
  FNewIntProp:= 99;
end;

end.


BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Adding properties to controls
« Reply #3 on: August 03, 2012, 09:48:07 am »
Thanks, Howard, that's going straight into my FPC/Lazarus notes document.

Do you mind if I put it up on the wiki as well?

Thanks!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

howardpc

  • Hero Member
  • *****
  • Posts: 3361
Re: Adding properties to controls
« Reply #4 on: August 03, 2012, 10:04:32 am »
Do you mind if I put it up on the wiki as well?
Not at all. There's too much mystery surrounding component creation and customisation. Of course there are pitfalls and I'm still trying to find out certain details myself (e. g. how to add full keyboard handling to a direct TCustomControl descendant). But the basic ideas are straightforward and logical, and should help beginners to start thinking in an OOP way, rather than hacking around misusing properties for purposes they are not designed for.
Howard

KpjComp

  • Hero Member
  • *****
  • Posts: 680
Re: Adding properties to controls
« Reply #5 on: August 03, 2012, 10:07:02 am »
For adding properties to designtime controls at runtime,  try having a look at implementing an Interposer Class..

Interposer Class -> http://www.lazarus.freepascal.org/index.php/topic,17633.0.html

dahaiou

  • New Member
  • *
  • Posts: 40
Re: Adding properties to controls
« Reply #6 on: August 03, 2012, 11:24:53 am »
Excellent answers. Big thanks everyone !

(Any other ideas or comments are still welcome of course)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Adding properties to controls
« Reply #7 on: August 03, 2012, 12:46:49 pm »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

dahaiou

  • New Member
  • *
  • Posts: 40
Re: Adding properties to controls
« Reply #8 on: August 03, 2012, 06:47:14 pm »
Hey, wouldn't it be a good idea to document the Interposer Class method on the wiki as well? It seems even simpler.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Adding properties to controls
« Reply #9 on: August 03, 2012, 06:49:06 pm »
Go ahead, anybody can edit the wiki :)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

dahaiou

  • New Member
  • *
  • Posts: 40
Re: Adding properties to controls
« Reply #10 on: August 04, 2012, 10:17:26 am »
I know, but I'm too much of a beginner and would likely get it wrong and mess up the Wiki.

Knipfty

  • Full Member
  • ***
  • Posts: 230
Re: Adding properties to controls
« Reply #11 on: August 06, 2012, 06:07:02 pm »
Add a comment on the wiki when you update it that you are a beginner and would appreciate someone to review and verify the info below.  I see no reasopn why you can't add what you know and let other come in and fix it later on.
32-bit Lazarus 1.0 FPC 2.6.0, 64-bit Win 7, Advantage DB 10.10