?
Solved

calling procedures and getting results!

Posted on 2003-03-25
4
Medium Priority
?
193 Views
Last Modified: 2010-04-04
i have a very long piece of code in my program which calculates the values from addition of different boxes from an invoice. i've got this procedure on two forms, one to create an invoice and one to amend it. what i need to do is call the procedure which is on form3 from form20! help! here is basically what i have so far and it does nothing.

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, .....

type
  TForm3 = class(TForm)
    Label10: TLabel;
    Label11: TLabel;
    .....
    procedure Timer1Timer(Sender: TObject);
    procedure Calculate1Click(Sender: TObject);
    procedure calculate;

private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form3: TForm3;

implementation

uses Unit2, Unit14, Unit17;

{$R *.dfm}

procedure tform3.calculate;

var
  dedit1, dedit2, dedit3, dedit4, dedit5, dedit6, dedit7, dedit8, dedit9,
  dedit10, dedit11, dedit12 :real;
  dedit13: currency;.....

begin

    if DBedit75.Text='' then ded1:= 0 else ded1:= strtoint(DBedit75.Text);
    if DBedit99.Text='' then ded2......
    ..... DBEdit163.Text:= currtostr(dedit13);
end;

procedure TForm3.Calculate1Click(Sender: TObject);
begin
        calculate;   <---- this is it called in form3
end;
....
----------------------------------------------------------------------------------------------------

unit Unit20;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, printers, Mask, DBCtrls, ExtCtrls, DB, ADODB, Menus;
type
  TForm20 = class(TForm)
    DBNavigator1: TDBNavigator;
    procedure MenuItem5Click(Sender: TObject);

private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form20: TForm20;

implementation

uses Unit14, unit3;

{$R *.dfm}

procedure TForm20.MenuItem10Click(Sender: TObject);

begin
    form3.calculate;    <---- this is it called in form20
end;
.....

THANKYOU!!!!
0
Comment
Question by:babbels4princess
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8202217
move this

var
 dedit1, dedit2, dedit3, dedit4, dedit5, dedit6, dedit7, dedit8, dedit9,
 dedit10, dedit11, dedit12 :real;
 dedit13: currency;.....


before the implementation keywordof your unit 3
-> make it global
(currently it is local and is discarded,
if the scope comes out of this procedure)

meikl ;-)
0
 
LVL 6

Accepted Solution

by:
PoeticAudio earned 60 total points
ID: 8204083
Yeah Here is how you call a procedure from a different form, this is just a sample procedure but it should help you get the picture...


unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Unit2 //<------
//Notice above you have to put the unit that your other form
//is in, in your uses clause, lets say Form2 is in Unit2

type
 TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
private
   { Private declarations }
 public
   { Public declarations }
   Function AddNums(x, y: integer): integer;
 end;
//Notice that you declare your method (function in this
//case) in your public declaration of this unit, anything
//in your public declarations can be called from any other                                
//unit (such as another unit with a form) now its "global"



Var
  Form1: TForm1;

implementation

{$R *.dfm}

//Notice I have to put Function TForm1.AddNums not just
//Function AddNums ...etc

Function TForm1.AddNums(x, y: integer): integer;
Begin
  Result := x + y;
end;


{
okay now say you have a button click event in a different form and you want to call the AddNumbs function you just do
...}

procedure TForm2.Button1Click(Sender: TObject);
Var
  XplusY: integer;
begin
  XPlusY := TForm1.AddNums(2, 2);
end;

end.
0
 

Author Comment

by:babbels4princess
ID: 8210133
ok, well i can call it but it doesn't seem to use the data of the second form. i call the procedure from form3 with this..

with form20 do form3.calculate;

but it uses the data from form3 instead of 'with form20'
0
 
LVL 1

Expert Comment

by:nafeelm
ID: 8230066

function TForm3.calculate(v1, v2, v3, v4: string) : double;
var netamt: double
begin
   if v1 <> '' then netamt:= netamt + strtofloat(v1);
   if v2 <> '' then netamt:= netamt + strtofloat(v2);
   if v3 <> '' then netamt:= netamt + strtofloat(v3);
   if v4 <> '' then netamt:= netamt + strtofloat(v4);

   Result:=netamt;
end;

procedure TForm3.Calculate1Click(Sender: TObject);
begin
  edit5.text:=floattostr(calculate(edit1.text, edit2.text, edit3.text, edit4.text));
end;


Now in Form20, add the Form20 in the uses clause under implementation

procedure TForm20.MenuItem10Click(Sender: TObject);
begin
   edit5.text:=floattostr(form3.calculate(edit1.text, edit2.text, edit3.text, edit4.text));
end;


considering that edit1, edit2, edit3, edit4, edit5 are TEdit components placed on both the forms.

if the above code works fine then you can replace the TEdits with TDBEdits.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

771 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