如何在2个窗体间调用函数?(10分)

  • 主题发起人 主题发起人 77578790
  • 开始时间 开始时间
7

77578790

Unregistered / Unconfirmed
GUEST, unregistred user!
如果unti1自定义一个function,那么unit2怎么调用它?
 
在Unit2中
implementation部分
Uses Unit1
 
怎么在unti2里面调用此function?
 
在Unit2的implementation后面加上:
uses Unit1;
或者用快捷键Alt+F11调出设置窗口.
 
在Unit2中
implementation下面代码中加入
Uses Unit1
并在调用中假如unit1是form1 则form1.function;
 
----------------unit1---------------
implementation
uses unit2;
{$R *.dfm}

function showmsg(s:string):string;
begin
edit1.text:=s;

end;
-------------------------------------------
-----------------unit2-----------------------
uses unit1;
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.showmsg('kkkkk');
end;
运行project提示unit2的showmsg没有定义.
 
----------------unit1---------------
implementation
uses unit2;
{$R *.dfm}
//此函数不是Form1所有的
//属于Unit1所有
function showmsg(s:string):string;
begin
Form1.edit1.text:=s;

end;
-------------------------------------------
-----------------unit2-----------------------
uses unit1;
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.showmsg('kkkkk');//这样调用出错
//改为
ShowMsg('kkkkkk');
end;
运行project提示unit2的showmsg没有定义.
 
Uses Unit1;
....

begin
Unit1.(输入小数点,就可以看到函数了名称列表了)
end;
 
最好不要调用uses....采用接口来实现。。。[:D]
 
superway你的方法在unit2还是提示showmsg没有定义,
royal1442你的方法也不行.
 
-----------------unit2-----------------------
uses unit1;
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.showmsg('kkkkk');//这样调用出错
//改为
ShowMsg('kkkkkk');
end;
运行project提示unit2的showmsg没有定义.

//提示invalid typecast
 
.......晕,估计是声明的位置问题:
//-------------------unit1-----------------------
type
TForm1 = class(TForm)
procedure pro1;
private
{ Private declarations }
procedure pro2;
public
{ Public declarations }
procedure pro3;
end;

procedure pro4;

var
Form1: TForm1;

implementation
{$R *.dfm}

procedure tform1.pro1;
begin
//
end;

procedure tform1.pro2;
begin
//
end;

procedure tform1.pro3;
begin
//
end;

procedure pro4;
begin
//
end;

//-------------------unit2-----------------------
implementation

uses Unit1
//先引用unit1;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.pro1;//可以引用
form1.pro2;//不可以引用
form1.pro3;//可以引用
pro4;//可以引用
end;
 
hs-kill你写的是过程(没有返回值),我要的是函数(有返回值)
 
。。。。。。。老兄,这有区别吗?你自己试试看不就知道了
 
Unit1中的函数(过程)一定要在 implementation 上面有声名才行!
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
[red]uses unit2;[/red]
{$R *.dfm}

function msg(s:string):string;
begin
form1.edit1.text:=s;
end;

end.
---------------------------unit2-------------------------------------------
unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;

implementation
[red]uses unit1;[/red]
{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
msg('kkkkk');

end;

end.


//--------------编译无法通过,提示:---------------------------------------------
[错误] Unit2.pas(28): Invalid typecast
[致命错误] Unit1.pas(23): Could not compile used unit 'Unit2.pas'
 
var
Form1: TForm1;
function msg(s:string):string;//先声名一下
implementation
[red]uses unit2;[/red]
{$R *.dfm}

function msg(s:string):string;
begin
form1.edit1.text:=s;
end;
 
多人接受答案了。
 
谢谢各位老大.
 
后退
顶部