communicate and share codes between frames and forms lazarus / 21-General ---------------------------------------------------------------------- Ericktux · 2021-01-05 01:38 hello friend, first of all happy new year. My question is the following, how can I send codes between frames and forms easily. I have: frame1 connected to form1 frame2 connected to form2 and I can't find a way to share codes between them because I get an error, I send them the code. FORM1 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 Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,  StdCtrls, Unit2, Unit3, Unit4; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;    Button3: TButton;    Button4: TButton;    Edit1: TEdit;    Panel1: TPanel;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);  private    { private declarations }   public    { public declarations }      procedure CerrarFrames;  end; var  Form1: TForm1;  Frame1: TFrame1;  Frame1EnUso: integer; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin    if Frame1EnUso = 1     then Exit;   Frame1 := TFrame1.Create(nil);  Frame1.Parent := form1;  Frame1.Align := alClient;  Frame1EnUso := 1; end; procedure TForm1.Button2Click(Sender: TObject);begin end;  procedure TForm1.Button3Click(Sender: TObject);begin  CerrarFrames;end; procedure TForm1.Button4Click(Sender: TObject);begin  Form2.Show;end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  CerrarFrames;  CloseAction := caFree;end; procedure TForm1.FormCreate(Sender: TObject);begin  Frame1EnUso := 0;  //Form2:= tForm2.Create(self);end; procedure TForm1.CerrarFrames;begin   //Frame1.Free;  FreeAndNil(Frame1);  Frame1EnUso := 0;end; end. FRAME1 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 Unit2; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, ExtCtrls, StdCtrls, Dialogs; type   { TFrame1 }   TFrame1 = class(TFrame)    Button1: TButton;    Button2: TButton;    Edit_frame1: TEdit;    Label1: TLabel;    procedure Button1Click(Sender: TObject);   private    { private declarations }  public    { public declarations }  end; implementation {$R *.lfm} { TFrame1 } procedure TFrame1.Button1Click(Sender: TObject);begin end;end. FORM2 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 Unit4; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls, Unit3; type   { TForm2 }   TForm2 = class(TForm)    Button2: TButton;    Button3: TButton;    Button4: TButton;    Edit2: TEdit;    Panel1: TPanel;     procedure Button1Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button4Click(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCreate(Sender: TObject);  private   public     procedure CerrarFrames2;  end; var  Form2: TForm2;  Frame2: TFrame2;  Frame2EnUso: integer; implementation {$R *.lfm} { TForm2 } procedure TForm2.CerrarFrames2;begin  FreeAndNil(Frame2);  Frame2EnUso := 0;end; procedure TForm2.Button1Click(Sender: TObject);begin  end; procedure TForm2.Button3Click(Sender: TObject);begin    if Frame2EnUso = 1     then Exit;   Frame2 := TFrame2.Create(nil);  Frame2.Parent := form2;  Frame2.Align := alClient;  Frame2EnUso := 1;end; procedure TForm2.Button4Click(Sender: TObject);begin  CerrarFrames2;end; procedure TForm2.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  CerrarFrames2;  //CloseAction := caFree;end; procedure TForm2.FormCreate(Sender: TObject);begin  Frame2EnUso := 0;end; end. FRAME2 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 Unit3; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, ExtCtrls, StdCtrls; type   { TFrame2 }   TFrame2 = class(TFrame)    Button1: TButton;    Button2: TButton;    Edit_frame2: TEdit;    Label1: TLabel;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure FrameClick(Sender: TObject);  private    { private declarations }  public     { public declarations }  end; implementation {$R *.lfm} { TFrame2 }  procedure TFrame2.Button1Click(Sender: TObject);begin end; procedure TFrame2.Button2Click(Sender: TObject);begin end; procedure TFrame2.FrameClick(Sender: TObject);begin end; end. I want to send code from form2 to frame1 (form1) as follows but I get an exception error 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 // I AM IN FORM2 uses   unit2;  // I add unit of frame1.... var  Form2: TForm2;  framesito1: TFrame1;   // I add this to use components from frame1  Frame2: TFrame2;  Frame2EnUso: integer; ...// I want to send a text from form2 to frame1 (form1) but I get exception errorframesito1.Edit_frame1.Text:=form2.Edit2.Text; I also want to send code from frame1 (form1) to frame2 (form2) but I still get an exception error 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 // I AM IN FRAME1 uses   unit3; // I add unit of frame2.... var  framesito2: TFrame2;   // I add this to use components from frame2 ... framesito2.Edit_frame2.Text:=Edit_frame1.Text; // I get exception error please help  I attach the example project ---------------------------------------------------------------------- jamie · 2021-01-05 02:36 If you are trying to share data between multiple forms then one way is to create a UNIT or lets call it a DataModule . that datamodule can be included in the USES section of any unit that you want to share with...  By changing anything in the data module all other units that also has that unit in its uses clause can see the changes..  Also, you can share a single TListimage to be used for all forms incase of common images being use to save on space.. etc..  If that isn't what you are looking for, you can scan the list of FORMS that are created/Active by accessing the Application.Forms. array. from that you can use the message system to post a message for cross form communications  If any of this interest you then I am sure plenty here will aid you in this direction ---------------------------------------------------------------------- Ericktux · 2021-01-05 02:53 Thank you very much friend for your advice, a question there will be an easy way to use the components of a frame from a form, for example: If I am in form1 and I want to see the value of the edit component of form2, I do it 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 procedure TForm1.Button1Click(Sender: TObject);begin        ShowMessage(form2.edit1.text);  end;   There is no easy way to see the values of a component in a frame from a form ? ---------------------------------------------------------------------- Martin_fr · 2021-01-05 03:13 Do not add the lines that you marked as "// I add this to use components from frame2" There is no need. Your unit1 has 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 var  Frame1 :TFrame1;And you set this var in your code, in "Button1Click" In unit1 you can access this var. In any other unit, you can add a 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 implementation // this line existsuses unit1;And the other unit will be able to access the variable too. Of course you need to check that it is already initialized (that the button had been clicked). QuoteCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i framesito1.Edit_frame1.Text:=form2.Edit2.Text;Do not do that... That is really hard to read, and will lead to accidental errors quickly. 1) Call your "Edit1_frame1" / "Edit2" something meaningful, like "UsernameEdit" (or whatever) 2) In TFrame1 add a property (and similar in Form2) 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  TFrame1 = class(TFrame)  private    { private declarations }  public    { public declarations }    property UserName: String read GetUserName write SetUsername; // no setter if you only read  end;And have the getter/setter access the UsernameEdit.Text and then all you need to copy the text is 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 frame1.Username := form2.Username ---------------------------------------------------------------------- egsuh · 2021-01-05 04:04 Try following,  assuming that you have included each unit in each other's uses clause, 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 // In form1,  Frame1:= TFrame1.Create(self); Frame1.Parent:= Self; // In from2,  Frame2:= TFrame2.Create(self); Frame2.Parent:= Self; // Calling Frame2  from Form1  Form2.Frame2.Edit1.Text := 'I''d like to set this text within Frame2 from Form1';    // or Form2.Frame2.Edit1.Text := Frame1.Edit1.Text; But if you want to call from within frame, it gets complicated. If you want to send message from Frame1 to Frame2, then you have to add all of  unit_of_form1, unit_of_form2, unit_of_frame2  to the uses clause of unit_of_frame1.  Then you will  need some anchors. 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 TFrame1 = class(TFrame)    Edit1: TEdit;    public        TargetControl: TControl;         procedure SendMessage (msg: string); end;  TForm1 = class(TForm)   private       Frame1: TFrame1; end;  procedure TFrame1.SendMessage(msg); begin      (TargetControl as TEdit).Text:= msg; end; procedure TForm1.Button1Click(Sender:TObject);begin      Frame1.TargetControl:= Form2.Frame2.Edit1;      Frame1.SendMessage(Frame1.Edit1); end;  //----------------- form2 TFrame2 = class(TFrame)    Edit1: TEdit;end;  TForm2 = class(TForm)   private       Frame2: TFrame2; end; I haven't tested my codes. There could be wrong codes. But please think over one by one what it tries to do. ---------------------------------------------------------------------- Ericktux · 2021-01-05 05:00 Apparently, direct communication between frames and forms is not easy ---------------------------------------------------------------------- MarkMLl · 2021-01-05 08:55 What /exactly/ are you trying to do? Do you appreciate that once you've populated a frame you're supposed to use the IDE to put it onto a standard form rather than create it manually? I've certainly not had problems using them, with the caveat that the content of a frame should be edited in the original frame, not on an app's form. MarkMLl ---------------------------------------------------------------------- mangakissa · 2021-01-05 10:21 Quote from: Ericktux on January 05, 2021, 06:00:06 amApparently, direct communication between frames and forms is not easy Because all forms are private initialized. The reason that TFrame can communicate with TForm is TFrame is declared in TForm. The Example of Martin_fr is also working with Tforms only. ---------------------------------------------------------------------- Ericktux · 2021-01-05 12:24 hello friends, what I want to do is: 1. form1 opens 2. form1 loads frame1 3. I open form2 from form1 (with "form2 .show") now what I want to do is retrieve or put a value in the edit component found in frame1 (form1), all from form2 ---------------------------------------------------------------------- lucamar · 2021-01-05 12:53 The easiest (and safest) way to do what you want is through properties in the form(s), the canonical example being how dialogs works. Take, for (a somewhat contrived) example, a (Open/Save)FileDialog: you don't access directy the edit control for the filename; instead you have a property Filename whose getter/setter take care of accessing the control and do whatever they need to return a filename. The same should be done in your own application: if Form2 needs to set/get some info from Form1, Form1 should have an appropiately named property through which this info can be got/set, no matter whether the underlying implementation is reading a control's text, idem after some validation, some other calculation, etc. The advantage of doing it this way is that you are then decoupling the access to the information from the inner method to obtain it and leaving the ultimate responsibility where it pertains: in TForm1, so you can change or expand the base implementation, e.g. using a TrackBar instead of a TEdit, or whatever, and all you'd have to do, then, is change the property's setter/getter to get the value from the new place, instead of having the specifics scattered all over the place. It also makes it easier to use that information; all you have to do from elsewhere is access "Form1.SomeProperty", without regard to where it's coming from, instead of having to know that the info comes (today!) specifically from "Form1.Frame5.TheFifthEdit.Text" ---------------------------------------------------------------------- MarkMLl · 2021-01-05 13:45 Quote from: Ericktux on January 05, 2021, 01:24:43 pmhello friends, what I want to do is: 1. form1 opens 2. form1 loads frame1 3. I open form2 from form1 (with "form2 .show") now what I want to do is retrieve or put a value in the edit component found in frame1 (form1), all from form2 Why aren't you putting frame1 on form1 at design time using the IDE? MarkMLl ---------------------------------------------------------------------- Ericktux · 2021-01-05 14:50 hello friend MarkMLl what happens is that frame1 will not be the only frame. ---------------------------------------------------------------------- MarkMLl · 2021-01-05 15:14 Quote from: Ericktux on January 05, 2021, 03:50:46 pmhello friend MarkMLl what happens is that frame1 will not be the only frame. All right, put frame2 on the form using the IDE as well. Or is there anything else important you're not telling us? MarkMLl ---------------------------------------------------------------------- Ericktux · 2021-01-05 15:52 what happens is that I am going to use several frames, and they will be loaded in execution with a button they can change from one frame to another frame. So far there is no problem, I can change them at runtime, the problem I have is that for example a frame has an edit component and I want to access the edit properties from another form (form2) but I cannot find an easy way to do what. ---------------------------------------------------------------------- Ericktux · 2021-01-05 15:55 To be honest, I'm new to frames, through examples I can change frames at runtime, but I'm having problems accessing the properties of the components that are in the frame from another form. ---------------------------------------------------------------------- MarkMLl · 2021-01-05 15:59 In that case put them all on the form using the IDE but only set one of them visible. Or use a multi-page notebook. Just /don't/ try to guess how the IDE implementers do things internally: it will end in tears and wasted time for everybody. MarkMLl ---------------------------------------------------------------------- Ericktux · 2021-01-11 05:37 hello friends, I found a solution to communicate the form2 with the frame1 embedded in form1.  I share code and project. form1: 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 Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Unit2, Unit3; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Button2: TButton;  Button3: TButton;    Frame111: TFrame123;    procedure Button1Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button3Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button2Click(Sender: TObject);begin  form1.Frame111.Edit1.Text:='hola';end; procedure TForm1.Button3Click(Sender: TObject);begin  if not Assigned(Frame111) thenbegin  Frame111 := TFrame123.Create(Self);  Frame111.Parent :=Form1;end;  Frame111.Show;  Frame111.Align:=alClient;end; procedure TForm1.Button1Click(Sender: TObject);begin  Form2.Show;end; end.   frame1 embedded in form1 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 Unit2; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, StdCtrls; type   { TFrame123 }   TFrame123 = class(TFrame)  Button1: TButton;    Edit1: TEdit;    procedure Button1Click(Sender: TObject);  private   public   end; implementation {$R *.lfm} { TFrame123 } procedure TFrame123.Button1Click(Sender: TObject);begin  Edit1.Text:='i am a frame';end; end.  form2 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 Unit3; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm2 }   TForm2 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form2: TForm2; implementationuses  Unit1; {$R *.lfm} { TForm2 } procedure TForm2.Button1Click(Sender: TObject);begin  Form1.Frame111.Edit1.Text:='estoy form2';end; end.  At the moment it is working well for me in windows.