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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How does one call a Procedure on another form? 3

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
A procedure (say) Procedure ABC on (say) TFormMyFormOne can be called from anywhere on
that form with simply ABC; anywhere on the form.

How can can I call that Procedure from (say) TFormMyFormTwo?

Thanks in advance.
 
Assuming the .pas file that contains the definition of TFormMyFormOne is called MyFormOne.Pas, then add MyFormOne to the uses clauses on the form that defines TFormMyFormTwo.

Further assuming that procedure ABC is defined as part of TFormMyFormOne in the public section and the variable declared is FormMyFormOne

Then you can use the qualified form FormMyFormOne.ABC

 
Huston we have a problem! I don't understand what I need to do.

I have MyFormTwo as follows.

Code:
unit MyForm[b]Two[/b];

interface

uses
  Windows, Messages, etc. etc.;

type
  TFormMyForm[b]Two[/b] = class(TForm)
    DBText2: TDBText;
  Etc.
  Etc.

  procedure BlahBlah1;
  procedure BlahBlah2;  


  private
    { Private declarations }
    procedure BlahBlah3;  

  public
    { Public declarations }
  end;

var
  frmTwo: TfrmTwo;

implementation

uses Stuff;

{$R *.dfm}

  [b]ABC[/b];//I need this to work here from MyForm[b]One[/b] below.

I have MyFormOne as follows.

Code:
unit MyForm[b]One[/b];

interface

uses
  Windows, Messages, etc. etc.;

type
  TFormMyForm[b]One[/b] = class(TForm)
    DBText2: TDBText;
  Etc. Etc

  procedure BlahBlah1;
  procedure BlahBlah2;  


  private
    { Private declarations }
    procedure BlahBlah3;  
    [b]procedure ABC;[/b]
  public
    { Public declarations }
  end;

var
  frmOne: TfrmOne;

implementation

uses Stuff;

{$R *.dfm}

[b]TFormMyFormOne.Procedure.ABC[/b];
begin
   Do wonderful things;
end;


[b]ABC[/b]  //Works perfectly here.

What do I put where to get ABC to work on MyFormTwo? [bigears]

Thanks in advance.
 
hi,

Code:
unit MyFormTwo;

interface

uses
  Windows, Messages, etc. etc.;

type
  TFormMyFormTwo = class(TForm)
    DBText2: TDBText;
  Etc.
  Etc.

  procedure BlahBlah1;
  procedure BlahBlah2;  


  private
    { Private declarations }
    procedure BlahBlah3;  

  public
    { Public declarations }
  end;

var
  frmTwo: TfrmTwo;

implementation

uses [b]MyFormOne[/b];

{$R *.dfm}

  [b]TFormMyFormOne.[/b]ABC;//I need this to work here from MyFormOne below.

cheers

--------------------------------------
What You See Is What You Get
 
There appears to be some confusion between the unit name, the type name, and the form name. The unit name goes in the uses clause, the type name is used when declaring the form name, and the form name is used when referencing stuff on the form.

In your case:
Unit name: MyFormOne
Type name: TFormMyFormOne
Form name: frmOne

See corrections in [red]red[/red]:
Code:
unit MyFormTwo;

interface

uses
  Windows, Messages, etc. etc.;

type
  TFormMyFormTwo = class(TForm)
    DBText2: TDBText;
  Etc.
  Etc.

  procedure BlahBlah1;
  procedure BlahBlah2;  


  private
    { Private declarations }
    procedure BlahBlah3;  

  public
    { Public declarations }
  end;

var
  frmTwo: TfrmTwo; [red] // should be TFormMyFormTwo[/red]

implementation

uses Stuff[red],MyFormOne[/red];

{$R *.dfm}

  [red]frmOne.[/red]ABC;//I need this to work here from MyFormOne below.

Code:
unit MyFormOne;

interface

uses
  Windows, Messages, etc. etc.;

type
  TFormMyFormOne = class(TForm)
    DBText2: TDBText;
  Etc. Etc

  procedure BlahBlah1;
  procedure BlahBlah2;  


  private
    { Private declarations }
    procedure BlahBlah3;  
    [red]//[/red]procedure ABC;[red]  should be public[/red] 
  public
    { Public declarations }
    [red]procedure ABC;  // called from other units[/red]
  end;

var
[red] // [/red]  frmOne: TfrmOne;[red]
  frmOne:TFormMyFormOne;[/red]

implementation

uses Stuff;

{$R *.dfm}

TFormMyFormOne.Procedure.ABC;
begin
   Do wonderful things;
end;


ABC  //Works perfectly here.


I'm not sure how you got it to compile with the line
Code:
 frmOne: TfrmOne;
when there is no definition for TfrmOne that I can see. (I only see TFormMyFormOne.) This may be a feature of Delphi with which I am not familiar.


 
Still No luck! [bigglasses]

>I'm not sure how you got it to compile with the line

Code:
  frmOne: TfrmOne;

Easy! :) [2thumbsup].
Delphi creates that line all by it's little self.

>This may be a feature of Delphi with which I am not familiar.
Must be.

Having made the corrections you suggest it won't compile.

To obviate confusion with form names these are the actual names.

FormOne is actually GL. Being its Unit name whilst the Property Name is frmGL
FormTwo is actually GenAccs. Being its Unit name whilst the Property Name is frmGenAccs

In GL I have
Code:
public
  DefineGLTable;
var
  frmGL: TfrmGL;

implementation

uses GenAccs;

{$R *.dfm}

procedure TfrmGL.DefineGLTable;
begin
  Does things
end;

[b]DefineGLTable;[/b] //Works perfectly.

In GenAccs I have
Code:
var
  frmGenAccs: TfrmGenAccs;

implementation

[b]uses GL;[/b]

{$R *.dfm}

  [b]GL.DefineGLTable;[/b]  // Get exceptions to this as below.

But when I try to compile I get exceptions

[Error] GenAccs.pas(81): Declaration expected but identifier 'GL' found
[Error] GenAccs.pas(1280): Undeclared identifier: 'DefineGLTable'



Don't understand what "identifier" it is wanting because it doesn't want one in GL.(See above).
and it seems that GenAccs cannot "see" GL.

 
you need to reference the FORM name, not the UNIT name when you are calling the function:
Code:
var
  frmGenAccs: TfrmGenAccs;

implementation

uses GL;

{$R *.dfm}

  frmGL.DefineGLTable;  // Get exceptions to this as below.



Leslie
 
I get two exceptions

[Error] GenAccs.pas(81): Declaration expected but identifier 'frmGL' found.
[Error] Whoever gave you that advice lies like cheap watch!

[bigsmile] Hee! Hee! (No offence intended!) [2thumbsup]
 
You need to put the line
Code:
frmGL.DefineGLTable;  // Get exceptions to this as below.
inside a procedure or function. For example, you might place it in the OnCreate event handler for a form. You cannot place executable statements outside a procedure or function.





Andrew
Hampshire, UK
 
Andrew is absolutely correct. I was led down the garden path when you said that this works:
Code:
[b]TFormMyFormOne.Procedure.ABC;[/b]
begin
   Do wonderful things;
end;


[b]ABC[/b]  //Works perfectly here.
since ABC does not appear to be inside a procedure or function, I don't understand how you could say "Works perfectly here" Unfortunately, I didn't recognize that problem at the time of my first post.

 
>I was led down the garden path when you said that this works:

1.
You weren't led up the garden path Zathras. It doeswork- in GL.
Which I initially called MyFormOneearlier in this thread but my use of the
word Formwas causing confusion (in this thread). So I changed these references
to what is actually used - being GL and GenAccs.

Have another careful look at I have MyFormOne as follows at Sep 11 2004
in this thread - remembering that I have since declared the procedure in public
where it belongs.


2.
>since ABC does not appear to be inside a procedure or function

Yes it does Andrew. Have another look at I have MyFormOne (GL in
fact) as follows at Sep 11 2004 in this thread


>For example, you might place it in the OnCreate event handler for a form.

That solved the problem!!

Thanks Andrew! You remain in "guru" category in my estimation!

The beautiful thing about this is one can obviously declare procedures which one intends
to use all-over-the-place in the public area of a DataModule: along with all the
various components which are repeatedly used belong. And for the same reason.

Case closed! Thanks everyone!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top