Forum > Databases

[SOLVED] Passing data back and forth between forms

(1/2) > >>

HopefulGuy:
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?

kapibara:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  frmB: TfrmB; implementation uses  uFormA; {$R *.lfm} { TfrmB } 

HopefulGuy:
Thanks! That's exactly what I needed!

MarkMLl:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

Navigation

[0] Message Index

[#] Next page

Go to full version