How to associate data with window controls lazarus / 21-General ---------------------------------------------------------------------- Marion · 2021-08-17 16:21 Some controls already have this like the TStringGrid or TTreeView lets you associate and object with the visible control. I want to associate data with a TTabSheet. I tried to subclass it but it got stuck in a circular reference. What is the best way to add data to a visible control? ---------------------------------------------------------------------- Warfley · 2021-08-17 16:41 The Tag property. Every Component has this property and it is of type PtrInt,  i.e. an Int that can fit a Pointer. You can simply add a pointer to the data (either as a class or as a pointer type) there and use this ---------------------------------------------------------------------- mas steindorff · 2021-08-17 19:56 Also check out helpers.  you can add several fields without modifying the base class https://wiki.freepascal.org/Helper_types#Multi-scope_helpers Mas ---------------------------------------------------------------------- lucamar · 2021-08-17 21:01 Quote from: mas steindorff on August 17, 2021, 09:56:44 pmAlso check out helpers.  you can add several fields without modifying the base class https://wiki.freepascal.org/Helper_types#Multi-scope_helpers Sorry but that's wrong: one of the miriad things you can not do in a class helper is introduce new fields; you have to derive a child class for that. Multi-scope helpers are quite a different kettle of fish ... ---------------------------------------------------------------------- mas steindorff · 2021-08-17 23:32 interesting, you can add functions, procedures, and constants but not variables.     learn something new every day. ---------------------------------------------------------------------- jamie · 2021-08-18 01:40 Quote from: Marion on August 17, 2021, 06:21:17 pmSome controls already have this like the TStringGrid or TTreeView lets you associate and object with the visible control. I want to associate data with a TTabSheet. I tried to subclass it but it got stuck in a circular reference. What is the best way to add data to a visible control? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TTabSheet = Class(ComCtrls.TtabSheet)  Public    MyData:Integer; //Could be a record too!  end;               You can create a unit that has just this code in it and when you include it in other units ensure it's after the ComCrtls unit, that should force the compiler to use this version of it instead.  Or if you are doing this all in a single unit then just put it at the top. At least this way you can still use the designer for the properties that exist in the original version. ---------------------------------------------------------------------- lucamar · 2021-08-18 04:44 Quote from: mas steindorff on August 18, 2021, 01:32:47 aminteresting, you can add functions, procedures, and constants but not variables.  learn something new every day.If you think a little about it it's quite logical enough: the restrictions refer basically to things that rather than simply add some features to the one at hand would in fact make of the helper a new class, like consructors, destructors, new fields (so o.c. no properties referring directly to fields), etc. In that tesiture you'd be better off deriving a child class and the restrictions enforce this. Helpers are a rather ... helpful feature (if you'll pardon the bad pun) but cannot (and shoudn't) replace proper inheritance: its main purpose (as I see it) is to easily extend an existing class by adding to it some "utility" methods/properties while leaving it basically as it is. Nothing more ... and nothing less. ---------------------------------------------------------------------- lucamar · 2021-08-18 04:55 Quote from: mas steindorff on August 18, 2021, 01:32:47 aminteresting, you can add functions, procedures, and constants but not variables.  learn something new every day.If you think a little about it it's quite logical enough: the restrictions refer basically to things that rather than simply add some features to the one at hand would in fact make of the helper a new class, like consructors, destructors, new fields (so o.c. no properties referring directly to fields), etc. In that tesiture you'd be better off deriving a child class and the restrictions enforce this. Helpers are a rather ... helpful feature (if you'll pardon the bad pun) but cannot (and shoudn't) replace proper inheritance: its main purpose (as I see it) is to easily extend an existing class by adding to it some "utility" methods/properties while leaving it basically as it is. Nothing more ... and nothing less. In the case at hand, one might use a helper to add a property with a getter/setter pair ensuring the correct conversion form the desired data type to/from the pointer stored in the Tag, thus allowing one to do: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SomeControl.MyInternalData := SomeData;instead of having to cast to/from a pointer:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SomeControl.Tag := @SomeData;{... or ...}SomeData := TSomeData(Pointer(SomeControl.Tag)^);which is more prone to difficult to eye-ball errors. ---------------------------------------------------------------------- Warfley · 2021-08-18 13:37 Quote from: mas steindorff on August 18, 2021, 01:32:47 aminteresting, you can add functions, procedures, and constants but not variables.  learn something new every day. Think of helpers as syntactic sugar. Basically a helper like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TTestHelper = class helper for TTest  procedure Foo;end; ... test := TTest.Create;test.Foo;Is nothing different than having the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TTestHelper_Foo(self: TTest);... test :=  TTest.Create;TTestHelper_Foo(test); It's just to make the code look nicer. It does not actually change the type. This would also not make any sense, as different units can include different helpers: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // Unit 1:TTestHelper = class helper for TTest  Foo: String;end; // Unit 2:TTestHelper = class helper for TTest  Foo: Integer;end;So let's say you create a TTest in Unit1 and pass it to a function in Unit2, what should Foo be? Even worse, now you can also have the following: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TTestChild = class(TTest)  Foo: Double;end;Now an instance of TTestChild  is also an instance of TTest (as it is derived from that class), but it already defines the field Foo differently then the helpers. This results in a lot of complications and contradictions. These could be resolved by restricting to having at most one Helper of a certain class and having Child classes have to extend on that helper, but with these restrictions there would be no difference to inheritance.