Recent

Author Topic: Linking visual control content to class fields  (Read 2098 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Linking visual control content to class fields
« on: September 17, 2019, 12:55:38 am »
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

  • Hero Member
  • *****
  • Posts: 6090
Re: Linking visual control content to class fields
« Reply #1 on: September 17, 2019, 02:04:09 am »
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;

The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Linking visual control content to class fields
« Reply #2 on: September 17, 2019, 09:15:10 am »
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?
You can use the Model-GUI-Mediator principle. You can find an article about it here.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #3 on: September 17, 2019, 09:21:38 am »
Quote
You 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][+][-]
  1. TMyEdit = class(TCustomMaskEdit)
  2.  .....
  3. end;  

Edson

  • Hero Member
  • *****
  • Posts: 1301
Re: Linking visual control content to class fields
« Reply #4 on: September 17, 2019, 10:15:08 pm »
Maybe you can use my library: https://github.com/t-edson/MiConfig

It is used to link controls to variables and variables to disk.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #5 on: September 18, 2019, 07:09:19 am »
@Edson

Thank you for your help. I cannot read Spanish? Portugese? but anyway I'll look into your code.

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Linking visual control content to class fields
« Reply #6 on: September 18, 2019, 10:48:05 am »
@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.
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #7 on: September 18, 2019, 11:58:39 am »
I didn't implement full Model-GUI-Mediator approach, but built a stripped down version like following.

Code: Pascal  [Select][+][-]
  1. unit fcontrollink;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.   TMyClass = class
  12.   public
  13.      Field1: string;
  14.   end;
  15.  
  16.   TForm1 = class(TForm)
  17.     Button1: TButton;
  18.     Button2: TButton;
  19.     Button3: TButton;
  20.     Edit1: TEdit;
  21.     procedure Button1Click(Sender: TObject);
  22.     procedure Button2Click(Sender: TObject);
  23.     procedure Button3Click(Sender: TObject);
  24.     procedure Edit1EditingDone(Sender: TObject);
  25.     procedure FormCreate(Sender: TObject);
  26.   private
  27.      FMyClass : TMyClass;
  28.  
  29.      function getF1: string;
  30.      procedure setF1(s: string);
  31.      procedure setMyClass(mc: TMyClass);
  32.  
  33.   public
  34.      property MyClass: TMyClass read FMyClass write setMyClass;
  35.      property F1: string read getF1 write setF1;
  36.   end;
  37.  
  38. var
  39.   Form1: TForm1;
  40.  
  41. implementation
  42.  
  43. {$R *.lfm}
  44.  
  45. procedure TForm1.Button1Click(Sender: TObject);
  46. var
  47.    anmc : TMyClass;
  48. begin
  49.    anmc := TMyClass.Create;
  50.  
  51.    anmc.Field1 := 'string field 1';
  52.  
  53.    MyClass := anmc;
  54. end;
  55.  
  56. procedure TForm1.Button2Click(Sender: TObject);
  57. begin
  58.    if MyClass <> nil then ShowMessage('Field1 is :' + MyClass.Field1) ;
  59. end;
  60.  
  61. procedure TForm1.Button3Click(Sender: TObject);
  62. begin
  63.    Randomize;
  64.    F1 := IntToStr(Trunc(Random(1000000)));
  65. end;
  66.  
  67.  
  68. procedure TForm1.Edit1EditingDone(Sender: TObject);
  69. begin
  70.    F1 := trim(Edit1.Text);
  71. end;
  72.  
  73. procedure TForm1.FormCreate(Sender: TObject);
  74. begin
  75.    MyClass := nil;
  76. end;
  77.  
  78. function TForm1.getF1: string;
  79. begin
  80.    if FMyClass <> nil then Result := FMyClass.Field1 else Result := '';
  81. end;
  82.  
  83. procedure TForm1.setF1(s: string);
  84. begin
  85.    if MyClass <> nil then MyClass.Field1:= s;
  86.    Edit1.Text := s;
  87. end;
  88.  
  89. procedure TForm1.setMyClass(mc: TMyClass);
  90. begin
  91.    if mc <> nil then begin
  92.      FMyClass := mc;
  93.      Edit1.Enabled := True;
  94.      Edit1.Text := FMyClass.Field1;
  95.    end
  96.    else  Edit1.Enabled := False;
  97. end;
  98.  
  99. end.
  100.  

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.
 
« Last Edit: September 18, 2019, 12:07:34 pm by egsuh »

devEric69

  • Hero Member
  • *****
  • Posts: 648
Re: Linking visual control content to class fields
« Reply #8 on: September 18, 2019, 12:31:15 pm »
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)... :) ?
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #9 on: September 19, 2019, 05:48:17 am »
@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

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #10 on: September 19, 2019, 05:52:25 am »
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

  • Hero Member
  • *****
  • Posts: 648
Re: Linking visual control content to class fields
« Reply #11 on: September 19, 2019, 10:02:32 am »
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][+][-]
  1. TForm1 = class(TForm)
  2. .../...
  3. procedure Button1Click(Sender: TObject);
  4. procedure Edit1EditingDone(Sender: TObject);
  5. .../...
  6.  

...all these published events exist, as if they were declared, upfront, like this...:

Code: Pascal  [Select][+][-]
  1. Form1.Button1Click:= @Button1.onClick;
  2. Form1.Edit1EditingDone:= @Edit1.onEdit1EditingDone;
  3.  

...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.
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

egsuh

  • Hero Member
  • *****
  • Posts: 1273
Re: Linking visual control content to class fields
« Reply #12 on: September 19, 2019, 10:35:35 am »
@devEric69,

Thank you for your advice. I'll think over it. If I have bothered you in any way, that was not my intention.

 

TinyPortal © 2005-2018