您好,2个窗体类的方法怎样相互调用? 谢谢(100分)

  • 主题发起人 主题发起人 wjlsnet
  • 开始时间 开始时间
W

wjlsnet

Unregistered / Unconfirmed
GUEST, unregistred user!
我在implementation中相互引用,但无法完成调用。 以下是举例, 如果 unit2 用使用 unit1
中的方法: thecs ,该怎样调用? 我写在public中也不行。 谢谢 ,还有,可以在unit2中
调用unit1中的定义的控件的方法么? 如:调用unit1中Button1的click事件 谢谢

---------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
// procedure thecs(var a:String); //写在这儿时不行
private
{ Private declarations }
public
procedure thecs(var a:String);
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
procedure TForm1.thecs(var a:String);
begin
a:=a+'1234567890';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption:='1';
end;
end.
------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Label1: TLabel;
Button1: TButton;
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
label1.Caption:=thecs('1'); //怎样调用?
end;
end.
 
------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Label1: TLabel;
Button1: TButton;
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
label1.Caption:=form1.thecs('1'); //怎样调用?
end;
end.
 
加Form1不行呀
 
加Form1为什么不行,提示是什么?
 
老大!你定义的是procedure 埃,不是function 当然不行
 
把 procedure thecs(var a:String); 改为
function thecs(var a:String) : string;
 
加Form1提示:

types of actual and formal var parameters munst be identical
 
改为Function也一样的。
 
unit 1中:
public
function thecs(var a:String):String;

function thecs(var a:String):String;
begin
a:=a+'1234567890';
result:=a;
end;

unit 2中:
...
//保证form1已创建。如果你没有用系统自动创建,则自己创建
//form1:=Tform1.create(nil);或其他方式都可以
label1.Caption:=form1.thecs('1'); //怎样调用?

 
你传一个字符串性的变量进去,
var a:string='a';
label1.Caption:=form1.thecs(a); //怎样调用?
或者,参数不要var声明
function thecs( a:String) : string
label1.Caption:=form1.thecs('1'); //怎样调用?

 
To 完颜康:

着应该没有影响
请大家继续指导
 
为什么不行呢?把这段一点不动的拷贝过去。
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function thecs(a:String) : string;
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

function TForm1.thecs(a: string) : string;
begin
a:=a+'1234567890';
result :=a;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
form2.Show;
end;

end.

unit Unit2;

interface

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

type
TForm2 = class(TForm)
Button1: TButton;
Label1: TLabel;
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
Label1.Caption :=form1.thecs('1');
end;

end.
 
tianjh007 :

你的代码在Unit2中有点瑕璧。

procedure TForm2.Button1Click(Sender: TObject);
var
S : String;
begin
S := 's';
Label1.Caption :=Form1.thecs(S);
end;


谢谢完颜友,谢谢大家。
 
多人接受答案了。
 
后退
顶部