基础问题(20分)

  • 主题发起人 xuefeiyang
  • 开始时间
X

xuefeiyang

Unregistered / Unconfirmed
GUEST, unregistred user!
比如说我在form1里有个函数
function cl(x:integer):integer;
begin
x:=x+1;
cl:=x;
end;

我想在form2中调用,如何写源码?
请写的详细些(包括在哪里声明),我这方面比较面,谢谢!
 
加一个申明在implementation之上。

function cl(x:integer):integer;

implementation
 

建议将函数放在一个新的unit中。
form2中uses即可。
 
unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation
uses unit2; //主要
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
self.caption := inttostr(cl(3));
end;

end.


另外注意一点
form1中function cl(x:integer):integer;
begin
x:=x+1;
cl:=x;
end;
要写在public中
 
up :) I'm late;
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function cl(x:integer):integer;
implementation
{$R *.DFM}
function cl(x:integer):integer;
begin
x:=x+1;
cl:=x;
end;

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
begin
showmessage(inttostr(form1.cl(strtoint(Edit1.text))));
end;
end.
给看看到底哪里不对?
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm)
private
function cl(x:integer):integer;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
////function cl(x:integer):integer;
implementation
{$R *.DFM}
function tform1.cl(x:integer):integer;
-----
begin
x:=x+1;
cl:=x;
end;
 
问题依旧
Undeclared identifier;'cl'
 
在Form2调用时用:text1.text:=inttostr(from1.c1(2));应该没问题[:)]
 
问题依旧
Undeclared identifier;'cl'
 

在form1中
public
function cl(x:integer):integer;

implementation

function Tform1.cl(x:integer):integer;
begin
x:=x+1;
cl:=x;
end;

在form2中调用
form1.cl(2)
 
to 影子
谢谢,问题解决。但还有点疑问,望不吝赐教。
还有一个类似的问题,只是把函数改成了过程,编译却出错:
Unsadisfied or extemal declaration:'Tmailt.jbxz2'
*jbxz2为过程,在mailt被应用,在另一窗体也要被调用
这样声明对么?
public
procedure jbxz2;
 

再看看你的代码,不会有错的。
Tmailt应该改为mailt?
 
form1:
public
function cl(x:integer):integer;
program aa;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.DFM}
function Tform1.cl(x:integer):integer;
begin
x:=x+1;
x:=x+strtoint(form2.edit1.text);
cl:=x;
end;
program aa;
begin
form2.edit1.text:=inttostr(form1.cl(strtoint(Edit1.text)));
end;
end.
////////////////////////////////////////////////////
form2:

procedure TForm2.Button1Click(Sender: TObject);
begin
edit1.text:=inttostr(form1.cl(strtoint(Edit1.text)));
end;
procedure TForm2.Button2Click(Sender: TObject);
begin
form1.aa;
end;

 

呵呵,过程的定义是用procedure。
过程除了没有返回值之外,其余的与函数是相同的。
procedure Tform1.aa;
begin
form2.edit1.text:=inttostr(form1.cl(strtoint(Edit1.text)));
end;

另外,form1与form2的成员变量乱用,代码会很难读取,维护也不方便。

 
不好意思,有些感冒,晕晕乎乎的
刚才也没细想就发出去了,让您费心了
嘻嘻
 
顶部