Access the main form from a subsidiary in Delphi

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

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 the use of Unit1 circular errors appear.

How to make Unit1 GLOBAL?

+3
source share
4 answers

As the other answers say, you should use one of the blocks in the implementation section.

, 'unit2', 'unit1' . , "TChiled", "Form1". , , "unit1" "unit2", "Form1: TForm1" . :

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;

unit1 Form1 TChiled TChiled:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chiled.Form1 := Self;
  Chiled.ShowMainFormVariable;
end;
+4

- Unit1 uses Unit2, .

, . , , - .

+3

, , Unit1 uses Unit2:

unit Unit2;
......
implementation

uses
  Unit1;
.....

uses Unit2, . Unit1 Unit2, Unit2 Unit1. . use .


, . . , Form1.Variable? Variable TForm1. Form1 TForm1. ?

, .

+2

( ) . , .

+1

All Articles