Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling procs in separate forms

Posted on 1998-01-08
6
Medium Priority
?
166 Views
Last Modified: 2010-04-04
Ok, I know Delphi is susposed to be able to do this (so I think), but I cannot get this to work. Form1/Unit1 is the primary form, and I want to call a procedure in Form2/unit2, without having Form2 showing. Lets say the procedure in form2 is called "runthis";
--------
Form1
uses unit2...
.
begin
  Form2.runthis;
.
.
.
-----------
Form2
.
.
.
procedure runthis;
begin
  messagedlg....
end;
.
.
This is a basic example, and I thought this would be a simple solution, but I can't seem to to get past the compiler no matter what I try. I have tried putting the procedue header in the implementation section on form2, but that doesn't seem to get past the compiler either.  Can I do this in Delphi?  
           Thanks
0
Comment
Question by:jexd99
6 Comments
 
LVL 5

Accepted Solution

by:
ronit051397 earned 200 total points
ID: 1355852
You cannot write Form2.runthis because, then the Form2 is needed.
1.Create 2 forms.
2. Place a button in the main form.
3. copy the following:

PROJECT
-------

program Project1;

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

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

FORM1
-----

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation
uses unit2;
{$R *.DFM}

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

end.

FORM2
-----

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;
procedure ShowShow;
var
  Form2: TForm2;

implementation

{$R *.DFM}
procedure ShowShow;
begin
  ShowMessage('This procedure is from form2');
end;
end.

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1355853
It will work fine if you put the Runthis procedure in the public part of form2. Normally it should be ok then.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1355854
In order to use the public procedures you need to load the
Form2, but he/she want's to use the procedure without loading the form.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:michael2010898
ID: 1355855
You can create form2 and then hide it and use it's procedures
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1355856
But s/he doesn't want to allocate extra memory for the form and it's components, just for a particular procedure.
0
 

Author Comment

by:jexd99
ID: 1355857
Hooray!  It worked. :-)  I was having such a tough time with something so easy.  It looks like my major problem was placing the procedure header in the wrong place.  Anyway, I'm just glad it worked, and it even works without creating the 2nd form! Cool. :)
Thanks to all of you for your comments.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question