Recent

Author Topic: How to use TForm1.memo from Unit2  (Read 4854 times)

clemmi

  • Jr. Member
  • **
  • Posts: 54
How to use TForm1.memo from Unit2
« on: May 04, 2014, 05:18:17 pm »
An example:
Application project with a "unit1" that contains "Form1", a "Memo1", and "uses unit2".

A "unit2" (no forms) with a "procedure writetomemo"

How can I have the "unit2" procedure to write to the memo1 in unit1?


Code: [Select]
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, unit2;
type
  { TForm1 }
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
  unit2.writetomemo();
end;
end.


Code: [Select]
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils;
procedure writetomemo;
implementation
procedure writetomemo;
begin
  memo1.text := 'hi';
end;
end.

I get error:
unit2.pas(17,8) Error: Identifier not found "memo1"
unit2.pas(22) Fatal: There were 1 errors compiling module, stopping


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: How to use TForm1.memo from Unit2
« Reply #1 on: May 04, 2014, 05:30:03 pm »
Code: [Select]
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils;
procedure writetomemo;
implementation
uses unit1;
procedure writetomemo;
begin
  memo1.text := 'hi';
end;
end.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1309
Re: How to use TForm1.memo from Unit2
« Reply #2 on: May 04, 2014, 06:16:42 pm »
minor change...
Code: [Select]
... 
    memo1.text := 'hi';
...

Should probably be
Code: [Select]
....
   form1.memo1.text := 'hi';
...
Lazarus Trunk/FPC latest fixes on Windows 11
  How to use the forum:  https://wiki.lazarus.freepascal.org/Forum

varg300

  • New Member
  • *
  • Posts: 37
Re: How to use TForm1.memo from Unit2
« Reply #3 on: May 04, 2014, 10:56:06 pm »
Unit1 must be added to 'Uses' in unit2, furthermore you must point to Form1 when writing to memo1: Form1.Memo1.Text:='Some text';
While pointing to a component that's declared in i diferent unit, you must also point to the form witch the component resides in.

Code: [Select]
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, unit1;

procedure writetomemo;
implementation
procedure writetomemo;
begin
  Form1.memo1.text := 'hi';
end;
end.

clemmi

  • Jr. Member
  • **
  • Posts: 54
Re: How to use TForm1.memo from Unit2
« Reply #4 on: May 05, 2014, 01:07:19 am »
I tried all three sugestions and this is what worked for me:
 :)

Code: [Select]
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Unit2;
type
  { TForm1 }
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
            end;
var
  Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
  Unit2.write2memo();
end;
end.

Code: [Select]
unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes, SysUtils;
procedure write2memo;
implementation
uses Unit1;
procedure write2memo;
begin
  Form1.memo1.text := 'hi';
end;
end.

Thanks a lot, now to implement this into my big code...
-cl

 

TinyPortal © 2005-2018