Adding properties to controls lazarus / 31-LCL ---------------------------------------------------------------------- dahaiou · 2012-08-03 06:43 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? ---------------------------------------------------------------------- jwdietrich · 2012-08-03 06:59 Why don't you use the TEdit's tag field? It is intended to store arbitrary values. ---------------------------------------------------------------------- howardpc · 2012-08-03 07:28 Quote from: jwdietrich on August 03, 2012, 08:59:36 amWhy 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 · 2012-08-03 07:48 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! ---------------------------------------------------------------------- howardpc · 2012-08-03 08:04 Quote from: BigChimp on August 03, 2012, 09:48:07 amDo 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 · 2012-08-03 08:07 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 · 2012-08-03 09:24 Excellent answers. Big thanks everyone ! (Any other ideas or comments are still welcome of course) ---------------------------------------------------------------------- BigChimp · 2012-08-03 10:46 Quote from: howardpc on August 03, 2012, 10:04:32 amQuote from: BigChimp on August 03, 2012, 09:48:07 amDo you mind if I put it up on the wiki as well? Not at all. Edited: http://wiki.lazarus.freepascal.org/Extending_the_IDE#Extending _existing_components Hope that's the right spot/content. Thanks. ---------------------------------------------------------------------- dahaiou · 2012-08-03 16:47 Hey, wouldn't it be a good idea to document the Interposer Class method on the wiki as well? It seems even simpler. ---------------------------------------------------------------------- BigChimp · 2012-08-03 16:49 Go ahead, anybody can edit the wiki ---------------------------------------------------------------------- dahaiou · 2012-08-04 08:17 I know, but I'm too much of a beginner and would likely get it wrong and mess up the Wiki. ---------------------------------------------------------------------- Knipfty · 2012-08-06 16:07 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.