Solved

Calling procs in separate forms

Posted on 1998-01-08
6
139 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now