[SOLVED] Passing data back and forth between forms lazarus / 16-Databases ---------------------------------------------------------------------- HopefulGuy · 2021-05-09 05:00 I guess I'm too used to Delphi. I have a program I'm working on with multiple forms. Form A has a button who's onClick event calls Form B. So naturally the unit file for Form A uses the unit file for Form B. BUT, Form B needs to change properties on Form A. In Delphi I'd just include Form A's unit in Form B. But Lazarus/FreePascal doesn't seem to want to let me do that. So how do I access a property of From A from Form B? I tried referencing Self.Parent, but the compiler doesn't recognize the properties in form A that way. I can't typecast as Form A because in order to do that Form B would need to know the format of Form A, which brings me right back to where I started. Help? ---------------------------------------------------------------------- fpcoder · 2021-05-09 05:47 To avoid circular reference, add Form A's unit to Form B's implementation uses instead of the interface part. Then you reference form A's properties with frmA.SomeProperty. 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  frmB: TfrmB; implementation uses  uFormA; {$R *.lfm} { TfrmB } ---------------------------------------------------------------------- HopefulGuy · 2021-05-09 08:37 Thanks! That's exactly what I needed! ---------------------------------------------------------------------- MarkMLl · 2021-05-09 08:42 You might still find that you have a circular reference in the form/class definition. You can fix that using a class helper, yell if you need an example. MarkMLl ---------------------------------------------------------------------- wp · 2021-05-09 11:04 I try to avoid such a situation from the beginning. When a form defines settings needed by another form then I never acces the form to read the settings but declare a variable in a third unit, and this unit is to be used by both forms. Example (untested): Suppose Form1 contains a Combobox in which a particular font size is selected, and this font size will be needed by another 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 Globals;interface var  GlobalFontSize: Integer = 10; implementation end. ----------------------------- unit Unit1; ... implementation uses  Unit2, Globals;   // Use the implementation "uses" whenever possible. procedure TForm1.Combobox1Change(Sender: TObject);begin  if Combobox1.ItemIndex > -1 then    // Store selected font size in global variable    GlobalFontSize := StrToInt(Combobox1.Items[Combobox1.ItemIndex]);end;  ------------------------------ unit Unit2; ... implementation uses  Globals; procedure TForm2.UpdateFontSize;begin  // do something with GlobalFontSize  // Read the font size from the global variable rather than from Form1's combobox.end; I know global variables are usually considered to be bad programming style. But here a global variable is a true solution to the circular reference issue which sooner or later may become unresolvable. ---------------------------------------------------------------------- MarkMLl · 2021-05-09 11:58 Quote from: wp on May 09, 2021, 01:04:21 pmI know global variables are usually considered to be bad programming style. But here a global variable is a true solution to the circular reference issue which sooner or later may become unresolvable. I think the "considered harmful" designation is unsupportable as long as Lazarus insists on representing each form by a global variable. MarkMLl ---------------------------------------------------------------------- jcmontherock · 2021-05-09 16:22 Another way: 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;    Interface             uses Unit2;    implementation    ...   var Font1: TFont;        procedure TForm1.FormCreate(Sender: TObject);    begin      PFont :=  @Font1;      ......    end;     ===================================================    unit Unit2;    implementation     var PFont: ^TFont;      // address of Font1 in Unit1           procedure TForm2.UpdateFontSize;    var fontSize: Single;    begin      fontSize := PFont^.Size;      .......      PFont^.Size := 9;      .......    end;