Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

calling procedures and getting results!

Posted on 2003-03-25
4
Medium Priority
?
195 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
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

564 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