Solved

Calling procs in separate forms

Posted on 1998-01-08
6
143 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 50 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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