Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you get 2 forms communicating both ways 2

Status
Not open for further replies.

TrustIssues

Programmer
Dec 22, 2004
4
GB
I know how to get 2 forms communicating in one way-by adding in the uses of the first form the unit of the second form but I really need to get 2 forms to communicate both ways. Is there a way of doing this?
Thanks
 
Yes, you can have a uses clause in the implementation section of a form.

But think carefully before you continue down this path. It is not usually necessary to structure an application that way.
 
Yes I understand that there may be other ways to do what I want to do without using several forms, but right now the project I'm making is on several forms and I could really do with a way to get the two forms to communicate both ways.
 
So, as I said, you can have a uses clause in the implementation section:
Code:
unit Unit1;

interface

uses
  Windows, ....;

type
  TForm1 = class(TForm)
    :
    : (etc.)
  end;

var Form1: TForm1;

implementation

{$R *.dfm}

uses
  Unit2;

// You can reference non-private members from Form2 here:
//   MyVar := Form2.SomeVar;

end.
And similarly in the other unit:
Code:
unit Unit2;

interface

uses
  Windows, ....;

type
  TForm2 = class(TForm)
    :
    : (etc.)
  end;

var Form2: TForm2;

implementation

{$R *.dfm}

uses
  Unit1;

// You can reference non-private members from Form1 here:
//   MyVar := Form1.SomeVar;

end.
Hope this helps.
 
What about writing somewhere else, in a text file for instance, information that is common to many forms. Each one reads and updates it as necessary.
 
I did try that way originally, with a uses clause in each unit, problem is you then get a circular data link error. Any other ways?
 
Ok. I've told you twice. Here is actual working code:

Please try it before asking again.
Code:
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};

{$R *.res}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.
Code:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.Text := Form2.Caption;
  Form2.Show;
end;

end.
Code:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

uses Unit1;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Edit1.Text := Form1.Caption;
  Form1.Show;
end;

end.
 
Ah OK I spotted what I did wrong. I didn't realise it makes a difference between adding something in the uses clause at the top of the screen with windows and that, instead of just making your own uses up like you did there. Thats how I got my circular unit reference. Thanks for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top