Recent

Author Topic: How to access objects in different form?  (Read 6329 times)

julgus

  • New Member
  • *
  • Posts: 29
How to access objects in different form?
« on: November 01, 2016, 08:49:25 pm »
I have a mainform which opens a subform. In my mainform I have a uses subform. Works perfectly. But how to access objects in my mainform from my subform?

I cant "use mainform" in my subform.  Some sort of declare I guess, certainly an easy question but I haven't found an answer.


wp

  • Hero Member
  • *****
  • Posts: 13489
Re: How to access objects in different form?
« Reply #1 on: November 01, 2016, 10:16:42 pm »
To avoid the circular references, put the "uses" after the implementation line (not interface).

BUT: This is not the right way to design a multiform-program, add one ore more forms and you will soon be in a noncompilable situation. You must try to keep forms separate. Ideally, the forms should not carry any logics, they just display data and provide the possibility of user input. Create one or more new units containing all declarations and data - let me call that "data unit". When you open a form, populate the controls with the data from the data unit. When the user edits data or when the form is closed copy the data from the controls back to the data unit. Calculations, ideally, should not be performed within the form but in separate units. In essence: Every form can "use" the data units, but the data unit must not "use" any form unit.

totya

  • Hero Member
  • *****
  • Posts: 722
Re: How to access objects in different form?
« Reply #2 on: November 01, 2016, 11:11:19 pm »
To avoid the circular references, put the "uses" after the implementation line (not interface).

WP master said true (as usually), simple example:

Edson

  • Hero Member
  • *****
  • Posts: 1328
Re: How to access objects in different form?
« Reply #3 on: November 01, 2016, 11:45:46 pm »
BUT: This is not the right way to design a multiform-program, add one ore more forms and you will soon be in a noncompilable situation. You must try to keep forms separate. Ideally, the forms should not carry any logics, they just display data and provide the possibility of user input.

Exactly. That's not a good design including forms in the IMPLEMENTATION uses. I recommend too, define a hierarchy in the work of the forms. In that case only the low level Units (Foms), like dialogs, must be in the INTERFACE uses.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

holmesshanea

  • Guest
Re: How to access objects in different form?
« Reply #4 on: November 02, 2016, 12:44:47 am »
You need to avoid circular references

So, in your main form (form1), if you have

Code: Pascal  [Select][+][-]
  1. implementation
  2.  
  3. {$R *.lfm}
  4.  
  5. uses Unit2;  

then in your secondary form, you will need to place the uses in the interface section

Code: Pascal  [Select][+][-]
  1. interface
  2.  
  3. uses
  4.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  5.   Unit1;  

see multi-demo.zip attached

I have a mainform which opens a subform. In my mainform I have a uses subform. Works perfectly. But how to access objects in my mainform from my subform?

I cant "use mainform" in my subform.  Some sort of declare I guess, certainly an easy question but I haven't found an answer.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: How to access objects in different form?
« Reply #5 on: November 02, 2016, 01:11:53 am »
...then in your secondary form, you will need to place the uses in the interface section
no, no, no and no.

In case you really must (but, i strongly advise to read wp's comments), you should always try to solve it first with the uses clause in the implementation section. That avoids circular references to begin with.

If you start adding units/forms to the uses clause of your units interface section then the circular issue becomes a problem pretty fast.

It is barely necessary for whatever situation for forms to start referencing each other from the interface section (although there are a few exceptions but those can be usually solved otherwise). There is alway the option to add a module in case of working with non-visual components and, such module also allows you store 'global' variables so that they can be referenced from whatever unit/form that includes it.
« Last Edit: November 02, 2016, 01:30:07 am by molly »

Thaddy

  • Hero Member
  • *****
  • Posts: 18975
  • Glad to be alive.
Re: How to access objects in different form?
« Reply #6 on: November 02, 2016, 02:11:48 pm »
Yes, no, no, no,
Furthermore the application class is always accessible from any child form, so:
Code: Pascal  [Select][+][-]
  1. procedure TForm333.Button1Click(Sender: TObject);  // idiotic amount of forms....
  2. begin
  3.   application.MainForm.Caption := 'test';
  4. end;

Works. It also works on any other control.
Good design? No,no,no,no. Listen to wp and molly.
« Last Edit: November 02, 2016, 02:13:59 pm by Thaddy »
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

 

TinyPortal © 2005-2018