Linking visual control content to class fields lazarus / 31-LCL ---------------------------------------------------------------------- egsuh · 2019-09-16 22:55 What would be the best way to link the content of visual components (eg. TEdit) with a field of a class defined by me, just like TDBEdit is linked to a dataset field? Should I write descendents of the visual components? Or does something similar exist already? ---------------------------------------------------------------------- jamie · 2019-09-17 00:04 I suppose you could have a Property or just a Function in your class that accepts a ready made control. That can be stored in an protected variable of that same type.. YourClass = Class fEdit :TEdit;  public Property ExternalEdit:TEdit Write Fedit; End; Remember to not delete this control unless you want to manage it.  It would need to be pre-created somewhere already...  ExternalEdit  := SomeEditContorINstance; ---------------------------------------------------------------------- PascalDragon · 2019-09-17 07:15 Quote from: egsuh on September 17, 2019, 12:55:38 amWhat would be the best way to link the content of visual components (eg. TEdit) with a field of a class defined by me, just like TDBEdit is linked to a dataset field? Should I write descendents of the visual components? Or does something similar exist already? You can use the Model-GUI-Mediator principle. You can find an article about it here. ---------------------------------------------------------------------- egsuh · 2019-09-17 07:21 QuoteYou can use the Model-GUI-Mediator principle. You can find an article about it here. This is what I'm looking for. I do not want to study any more, but I have to.  Thank you for your advice.  I was thinking over writing something like 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 TMyEdit = class(TCustomMaskEdit) .....end; ---------------------------------------------------------------------- Edson · 2019-09-17 20:15 Maybe you can use my library: https://github.com/t-edson/MiConfig It is used to link controls to variables and variables to disk. ---------------------------------------------------------------------- egsuh · 2019-09-18 05:09 @Edson Thank you for your help. I cannot read Spanish? Portugese? but anyway I'll look into your code. ---------------------------------------------------------------------- devEric69 · 2019-09-18 08:48 @PascalDragon: thank you for sharing the link off the article which is very didactic. @egsuh: for information, the TForm is a design pattern Mediator (one part of the Model-GUI-Mediator), between all the controls placed on it. ---------------------------------------------------------------------- egsuh · 2019-09-18 09:58 I didn't implement full Model-GUI-Mediator approach, but built a stripped down version like 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 unit fcontrollink; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type  TMyClass = class  public     Field1: string;  end;   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    Edit1: TEdit;  procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Edit1EditingDone(Sender: TObject);    procedure FormCreate(Sender: TObject);  private     FMyClass : TMyClass;      function getF1: string;     procedure setF1(s: string);     procedure setMyClass(mc: TMyClass);   public     property MyClass: TMyClass read FMyClass write setMyClass;     property F1: string read getF1 write setF1;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);var   anmc : TMyClass;begin   anmc := TMyClass.Create;    anmc.Field1 := 'string field 1';    MyClass := anmc;end; procedure TForm1.Button2Click(Sender: TObject);begin   if MyClass <> nil then ShowMessage('Field1 is :' + MyClass.Field1) ;end; procedure TForm1.Button3Click(Sender: TObject);begin   Randomize;   F1 := IntToStr(Trunc(Random(1000000)));end;  procedure TForm1.Edit1EditingDone(Sender: TObject);begin   F1 := trim(Edit1.Text);end; procedure TForm1.FormCreate(Sender: TObject);begin   MyClass := nil;end; function TForm1.getF1: string;begin   if FMyClass <> nil then Result := FMyClass.Field1 else Result := '';end; procedure TForm1.setF1(s: string);begin   if MyClass <> nil then MyClass.Field1:= s;   Edit1.Text := s;end; procedure TForm1.setMyClass(mc: TMyClass);begin   if mc <> nil then begin     FMyClass := mc;     Edit1.Enabled := True;     Edit1.Text := FMyClass.Field1;   end   else  Edit1.Enabled := False;end; end.  This mimicks the behavior of Model-GUI-Mediator just using "setting" properties. When Button1 is clicked, a new object is assigned to TForm.FMyClass, and controls are filled. When editing is done at the controls then the content of field is changed. Button2 is to show the content of field content. Button3 is for case of assigning values programmatically.  Well, this is probably the most BASIC of OOP. Anyway welcome any comments. ---------------------------------------------------------------------- devEric69 · 2019-09-18 10:31 If it does the job, why complicate (if you don't have a compelling need for a two-way instant synchronization mechanism, when there's a modification of TMyClass.Field1, or of a visual control that displays TMyClass.Field1)... ? ---------------------------------------------------------------------- egsuh · 2019-09-19 03:48 @devEric69 I'm not clear what you mean, but I'm just thinking on my own application, which is not much complicated. I know that the full Model-GUI-Mediator model would be necessary when there are many fields that must be synchronized at once.  I'm looking for ways to minimize the scale. ---------------------------------------------------------------------- egsuh · 2019-09-19 03:52 But minimizing the scale just for current application does not seem to be good idea, thinking that one of the reasons for OOP is for future maintenance. So mediators should be defined from the start for Model objects. ---------------------------------------------------------------------- devEric69 · 2019-09-19 08:02 Quote@egsuh: for information, the TForm is a design pattern Mediator (one part of the Model-GUI-Mediator), between all the controls placed on it. Quote@devEric69 : I'm not clear what you mean All I wanted to say, is that the TForm is a mediator between all the controls, of which it's their parent...: 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 TForm1 = class(TForm).../...procedure Button1Click(Sender: TObject);procedure Edit1EditingDone(Sender: TObject);.../...  ...all these published events exist, as if they were declared, upfront, 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 Form1.Button1Click:= @Button1.onClick;Form1.Edit1EditingDone:= @Edit1.onEdit1EditingDone;  ...so, the TForm is aware of all the events of all its controls, because the TForm centralize them, like a mediator, which allows it to arbitrate their {re-}orders \ precedences \ interactions, ... coding the events centralized within the mediator. I didn't want to say anything more. ---------------------------------------------------------------------- egsuh · 2019-09-19 08:35 @devEric69, Thank you for your advice. I'll think over it. If I have bothered you in any way, that was not my intention.