Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access main form from child unit in Delphi

I want to access a main form variable from a class that is called from the main from. Something like this:

Unit1:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,Unit2, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;
var
  Form1: TForm1;
  Chiled:TChiled;
const
 Variable = 'dsadas';
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chiled.ShowMainFormVariable;
end;

end.

Unit2:

unit Unit2;

interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

  type
  TChiled = class
  private
  public
    procedure ShowMainFormVariable;
  end;
var
  Form1: TForm1;
implementation

procedure TChiled.ShowMainFormVariable;
begin
  ShowMessage(Form1.Variable);
end;
end.

if in Unit2 i add to uses Unit1 an circular errors pops up.

How to make the Unit1 to be GLOBAL?

like image 884
TreantBG Avatar asked Dec 12 '22 03:12

TreantBG


2 Answers

As other answers tell, you should use one of the units in implementation section.

Suppose you chose in 'unit2' you'd use 'unit1' in implementation. then you need to devise a mechanism to tell the 'TChiled' how to access 'Form1'. That's because since you haven't used 'unit1' in interface section of 'unit2', you cannot declare the 'Form1:TForm1' variable in interface section. Below is just one possible solution:

unit2

type
  TChiled = class
  private
    FForm1: TForm;
  public
    procedure ShowMainFormVariable;
    property Form1: TForm write FForm1;
  end;

implementation

uses
  unit1;

procedure TChild.ShowMainFormVariable;
begin
  ShowMessage((FForm1 as TForm1).Variable);
end;

then in unit1 you can set the Form1 property of TChiled before calling TChiled's method:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chiled.Form1 := Self;
  Chiled.ShowMainFormVariable;
end;
like image 195
Sertac Akyuz Avatar answered Dec 31 '22 20:12

Sertac Akyuz


the simplest solution is to add Unit1 to a uses clause inside Unit2's implementation section as this gets around the circular reference.

However I'd suggest that this design is flawed. It is hard to see what you are trying to achieve with the sample code so it is difficult to offer any real advice.

like image 26
John Pickup Avatar answered Dec 31 '22 19:12

John Pickup